预期的 Java 方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16418193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Method Call Expected
提问by Seb Welch
This is a java program with two buttons used to change an integer value and display it. However in IntelliJIDEA the two lines with
这是一个带有两个按钮的 Java 程序,用于更改整数值并显示它。但是在 IntelliJIDEA 中,两行
increase.addActionListener(incListener());
decrease.addActionListener(decListener());
keep displaying errors 'Method call expected'.
继续显示错误“预期方法调用”。
I am not sure what to do to fix this.
我不知道该怎么做才能解决这个问题。
Any help will be greatly appreciated
任何帮助将不胜感激
Thanks
谢谢
Note: the full code is attached below.
注意:完整代码附在下面。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JDialog {
public JPanel contentPane;
public JButton decrease;
public JButton increase;
public JLabel label;
public int number;
public Main() {
setContentPane(contentPane);
setModal(true);
increase = new JButton();
decrease = new JButton();
increase.addActionListener(incListener());
decrease.addActionListener(decListener());
number = 50;
label = new JLabel();
}
public class incListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
number++;
label.setText("" + number);
}
}
public class decListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
number--;
label.setText("" + number);
}
}
public static void main(String[] args) {
Main dialog = new Main();
dialog.pack();
dialog.setVisible(true);
System.exit(0);
}
}
采纳答案by Arnaud Denoyelle
incListener and declListener are classes, not methods.
incListener 和 declListener 是类,而不是方法。
Try
尝试
increase.addActionListener(new incListener());
btw, rename your classes names to make them start with an uppercase
顺便说一句,重命名您的类名称,使它们以大写开头
回答by Simulant
substitute the lines with
用
increase.addActionListener( new incListener());
decrease.addActionListener( new decListener());
回答by PurkkaKoodari
It's simple: use new incListener()
instead of incListener()
. The later is trying to call a methodnamed incListener
, the former creates an objectfrom the class incListener
, which is what we want.
很简单:用new incListener()
代替incListener()
. 后者试图调用一个名为的方法incListener
,前者从类中创建一个对象incListener
,这正是我们想要的。
回答by Manitra
incListener and decListener are a classes but not a methods, so you must call new to use them, try this:
incListener 和 decListener 是一个类而不是一个方法,所以你必须调用 new 来使用它们,试试这个:
increase.addActionListener(new incListener()); decrease.addActionListener(new decListener());
增加.addActionListener(new incListener()); 减少.addActionListener(new decListener());
sorry for my bad english
对不起,我的英语不好
回答by Sam
Make these changes:
进行这些更改:
public Main() {
contentPane = new JPanel();
setContentPane(contentPane);
setModal(true);
increase = new JButton("inc");
decrease = new JButton("dec");
contentPane.add(increase);
contentPane.add(decrease);
increase.addActionListener(new incListener());
decrease.addActionListener(new decListener());
number = 50;
label = new JLabel(number+"");
contentPane.add(label);
}
回答by Vikas Singh
Whenever a string object is created using newoperator a new object is created which is what your program is looking for. The following link is useful in learning about the difference between a string and a new string. What is the difference between "text" and new String("text")?
每当使用new运算符创建字符串对象时,都会创建一个新对象,这正是您的程序正在寻找的对象。以下链接有助于了解字符串和新字符串之间的区别。 "text" 和 new String("text") 有什么区别?
回答by Cory
It's sad but I had to Google this same error... I was staring at a method that returned a class. I left off the new
operator.
很难过,但我不得不用谷歌搜索同样的错误......我盯着一个返回一个类的方法。我离开了new
接线员。
return <class>(<parameters>)
vs
return new <class>(<parameters>)
return <class>(<parameters>)
对比
return new <class>(<parameters>)