所以我在某处找到了一个简单计算器的网格布局,我认为看看我是否可以开始工作会很有趣。
问题是,在我开始搞乱它添加actionlisteners等按钮之后,我的控制台就跟着我了:
错误:请在CalcMain.CalculatorMain类中找不到主要方法 将main方法定义为:public static void main(String [] args) 或JavaFX应用程序类必须扩展 javafx.application.Application
我在编码方面仍然相当新,所以我不完全确定这意味着什么,以及我应该如何修复它。
在我实现ActionListener并关闭
后弹出错误Calc.add(new JButton(buttons[i]));
与
test[i] = new JButton(buttons[i]);
test[i].addActionListener(this);
test[i].setActionCommand(buttons[i]);
我搞砸了什么,我该怎么做才能解决它?任何帮助表示赞赏!
提前致谢!
public class CalculatorMain extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public CalculatorMain() {
initUI();
}
public final void initUI() {
JPanel Calc = new JPanel();
Calc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Calc.setLayout(new GridLayout(5, 4, 5, 5));
String[] buttons = {
"Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
};
JButton[] test = new JButton[19];
for (int i = 0; i < buttons.length; i++) {
if (i == 2)
Calc.add(new JLabel(buttons[i]));
else
test[i] = new JButton(buttons[i]);
test[i].addActionListener(this);
test[i].setActionCommand(buttons[i]);
}
add(Calc);
setTitle("CalculatorDemo");
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CalculatorMain demo = new CalculatorMain();
demo.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
if(name.equals("1")) {
System.out.println("No");
}
else if(name.equals("2")) {
System.out.println("Yes");
}
}
}