我修正了错误,但我想问另一种方法来修复此错误。 试图制作一个基本的计算器,现在我正在与听众合作。当我尝试调用一个按钮时,它表示它无法对非静态字段进行静态引用。这是主要的类代码:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalcComb {
JPanel windowContent;
JFormattedTextField displayField;
static JButton button0;
static JButton button1;
static JButton button2;
static JButton button3;
static JButton button4;
static JButton button5;
static JButton button6;
static JButton button7;
static JButton button8;
static JButton button9;
static JButton buttonPoint;
static JButton buttonEqual;
static JButton buttonplus;
static JButton buttonminus;
static JButton buttonclear;
JPanel p1;
JPanel p2;
public CalcComb() {
windowContent = new JPanel();
BorderLayout bl = new BorderLayout();
windowContent.setLayout(bl);
displayField = new JFormattedTextField(0);
displayField.setHorizontalAlignment(SwingConstants.RIGHT);
windowContent.add("North", displayField);
button0=new JButton("0");
button1=new JButton("1");
button2=new JButton("2");
button3=new JButton("3");
button4=new JButton("4");
button5=new JButton("5");
button6=new JButton("6");
button7=new JButton("7");
button8=new JButton("8");
button9=new JButton("9");
buttonplus=new JButton("+");
buttonminus=new JButton("-");
buttonclear=new JButton("C");
buttonPoint = new JButton(".");
buttonEqual=new JButton("=");
p1 = new JPanel();
p2 = new JPanel();
GridLayout gl2 = new GridLayout(3, 1);
GridLayout gl = new GridLayout(3, 3);
p1.setLayout(gl);
p2.setLayout(gl2);
p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(button0);
p1.add(buttonPoint);
p1.add(buttonEqual);
p2.add(buttonplus);
p2.add(buttonminus);
p2.add(buttonclear);
windowContent.add("West", p2);
windowContent.add("Center", p1);
JFrame frame = new JFrame("Calculator");
frame.setSize(400, 400);
frame.setContentPane(windowContent);
frame.pack();
frame.setVisible(true);
CalcEngine engine = new CalcEngine();
button0.addActionListener(engine);
button1.addActionListener(engine);
button2.addActionListener(engine);
button3.addActionListener(engine);
button4.addActionListener(engine);
button5.addActionListener(engine);
button6.addActionListener(engine);
button7.addActionListener(engine);
button8.addActionListener(engine);
button9.addActionListener(engine);
buttonplus.addActionListener(engine);
}
public static void main(String[] args) {
CalcComb calc = new CalcComb();
}
}
这是我的监听器类代码:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CalcEngine implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
JButton clickedButton = (JButton) e.getSource();
String clickedButtonLabel = clickedButton.getText();
if(e.getSource() instanceof JButton) {
JOptionPane.showConfirmDialog(null, "You pressed " + clickedButtonLabel, "Just a test", JOptionPane.PLAIN_MESSAGE);
if (clickedButton == CalcComb.buttonplus) {
JOptionPane.showConfirmDialog(null, "Congratulations", "Just one more test", JOptionPane.PLAIN_MESSAGE);
}
}
}
}
在每个JButton之前没有单词static的任何想法?
答案 0 :(得分:3)
您不应使用static关键字来访问CalcComb
类中的按钮。您必须使用setActionCommand()
方法为您的JButtons设置每个按钮的唯一字符串。然后在actionPerformed()
中,您可以使用getActionCommand()来获取之前设置的唯一字符串,并确定按下哪个按钮。无需在CalcComb
类中使用静态引用:
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "PLUS":
// DO SOMTHING:
break;
case "MULT":
// DO SOMTHING:
break;
default:
break;
}
}
};
JButton b = new JButton("+");
b.setActionCommand("PLUS");
b.addActionListener(actionListener);
JButton b1 = new JButton("*");
b1.setActionCommand("MULT");
b1.addActionListener(actionListener);
此外,您可能希望避免在init()方法或构造函数中定义ActionListener实现。一个好的做法是你的CalcComb
类实现ActionListener intreface,这样你就可以在CalcComb
类中使用actionPerformed方法,但无论如何使用setActionCommand()
和getActionCommand()
方法比使用类的成员变量检查引用检查更好:
public class CalcComb implements ActionListener{
...
...
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "PLUS":
// DO SOMETHING:
break;
case "MULT":
// DO SOMETHING:
break;
default:
break;
}
}
}
祝你好运。
答案 1 :(得分:0)
您可以使用以下流程:
if (clickedButton == new CalcComb().buttonplus) {
JOptionPane.showConfirmDialog(null, "Congratulations", "Just one more test", JOptionPane.PLAIN_MESSAGE);
}