Java方法调用预期 [英] Java Method Call Expected
本文介绍了Java方法调用预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个java程序,有两个按钮用于更改整数值并显示它。
但是在IntelliJIDEA中,两行
This is a java program with two buttons used to change an integer value and display it. However in IntelliJIDEA the two lines with
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);
}
}
推荐答案
incListener和declListener是类,而不是方法。
incListener and declListener are classes, not methods.
尝试
increase.addActionListener(new incListener());
btw,重命名你的班级名称,使它们以大写字母开头
btw, rename your classes names to make them start with an uppercase
这篇关于Java方法调用预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文