Java 无法对非静态字段进行静态引用(操作侦听器)

Java 无法对非静态字段进行静态引用(操作侦听器),java,static,listener,jbutton,actionlistener,Java,Static,Listener,Jbutton,Actionlistener,我修正了错误,但我想问另一种方法来修正这个错误。 我试着做一个基本的计算器,现在我正在和听众一起工作。当我试图调用一个按钮时,它说它不能对非静态字段进行静态引用。以下是主要的类代码: import javax.swing.*; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; publ

我修正了错误,但我想问另一种方法来修正这个错误。 我试着做一个基本的计算器,现在我正在和听众一起工作。当我试图调用一个按钮时,它说它不能对非静态字段进行静态引用。以下是主要的类代码:

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);
            }
        }
    }
}
以下是我的侦听器类代码:

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,您有没有办法解决这个问题?

您不应该使用static关键字访问
CalcComb
类中的按钮。您必须使用JButtons的
setActionCommand()
方法为每个按钮设置唯一的字符串。然后在
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;
        } 
    }
}

祝你好运。

你不应该使用static关键字访问
CalcComb
类中的按钮。您必须使用JButtons的
setActionCommand()
方法为每个按钮设置唯一的字符串。然后在
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;
        } 
    }
}

祝你好运。

你不应该使用static关键字访问
CalcComb
类中的按钮。您必须使用JButtons的
setActionCommand()
方法为每个按钮设置唯一的字符串。然后在
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;
        } 
    }
}

祝你好运。

你不应该使用static关键字访问
CalcComb
类中的按钮。您必须使用JButtons的
setActionCommand()
方法为每个按钮设置唯一的字符串。然后在
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;
        } 
    }
}

祝您好运。

您可以使用以下流程:

 if (clickedButton == new CalcComb().buttonplus) {
            JOptionPane.showConfirmDialog(null, "Congratulations", "Just one  more test", JOptionPane.PLAIN_MESSAGE);
        }

您可以使用以下过程:

 if (clickedButton == new CalcComb().buttonplus) {
            JOptionPane.showConfirmDialog(null, "Congratulations", "Just one  more test", JOptionPane.PLAIN_MESSAGE);
        }

您可以使用以下过程:

 if (clickedButton == new CalcComb().buttonplus) {
            JOptionPane.showConfirmDialog(null, "Congratulations", "Just one  more test", JOptionPane.PLAIN_MESSAGE);
        }

您可以使用以下过程:

 if (clickedButton == new CalcComb().buttonplus) {
            JOptionPane.showConfirmDialog(null, "Congratulations", "Just one  more test", JOptionPane.PLAIN_MESSAGE);
        }

为什么要用static关键字修改按钮?JButton clickedButton=(JButton)e.getSource()做了什么意思?我认为你的ActionListener不应该包含按钮。相反,我会将ActionListener定义为一个函数,而不是一个类,并尝试类似这样的操作:
if(e.getSource()==this.button0)
为什么要用static关键字修改按钮?JButton clickedButton=(JButton)e.getSource()是什么意思?我认为你的ActionListener不应该包含按钮。相反,我会将ActionListener定义为一个函数,而不是一个类,并尝试类似这样的操作:
if(e.getSource()==this.button0)
为什么要用static关键字修改按钮?JButton clickedButton=(JButton)e.getSource()是什么意思?我认为你的ActionListener不应该包含按钮。相反,我会将ActionListener定义为一个函数,而不是一个类,并尝试类似这样的操作:
if(e.getSource()==this.button0)
为什么要用static关键字修改按钮?JButton clickedButton=(JButton)e.getSource()是什么意思?我认为你的ActionListener不应该包含按钮。相反,我会将ActionListener定义为一个函数,而不是一个类,并尝试类似这样的操作:
if(e.getSource()==this.button0)
clickedButton==newCalccomb().buttonplus将不是true,因为每次发生操作时,您都在创建CalcComb类的新实例,==运算符无法将buttonplus的引用与actionEvent的源匹配。我认为clickedButton==new CalcComb()