Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多按钮Swing的ActionListener_Java_Swing_Static_Compiler Errors_Actionlistener - Fatal编程技术网

Java 多按钮Swing的ActionListener

Java 多按钮Swing的ActionListener,java,swing,static,compiler-errors,actionlistener,Java,Swing,Static,Compiler Errors,Actionlistener,我想使用相同的EventHandler处理多个按钮的操作。单击按钮时,标签的文本将相应更改 此代码显示了 public class string implements ActionListener{ JLabel jlab; public void actionPerformed(ActionEvent Ae){ jlab.setText(Ae.getActionCommand()); } public static void m

我想使用相同的EventHandler处理多个按钮的操作。单击按钮时,标签的文本将相应更改

此代码显示了

public class string implements ActionListener{

    JLabel jlab;

    public void actionPerformed(ActionEvent Ae){
         jlab.setText(Ae.getActionCommand());
     }

        public static void main(String args[]){
            JFrame j = new JFrame("HP");
            j.setSize(300,300);
            j.setLayout(new FlowLayout());
            j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
            j.setVisible(true);    

        jlab=new JLabel("Here");
        j.add(jlab);

            JButton j1=new JButton("Button1");
            j1.setActionCommand("Your pressed Button 1");

            j1.addActionListener(this );

            j.add(j1);

            JButton j2=new JButton("Button2");
            j2.setActionCommand("Your pressed Button 1");
            j2.addActionListener(this );
            j.add(j2);
    }
   }

如何更正此代码?

了解变量是什么以及它是如何工作的非常重要。不能从非静态上下文引用静态变量

Non static variable this cannot be referenced from a static context. 

您的问题有两种解决方案:

  • 使用构造函数

    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.awt.FlowLayout;
    
    public class Stackoverflow implements ActionListener{
    
        private JLabel jlab; /* It's a good practice to make your variables private or protected */
    
    
    
        public static void main(String args[]){
            new Stackoverflow();
        }
        public Stackoverflow(){
                JFrame j = new JFrame("HP");
                j.setSize(300,300);
                j.setLayout(new FlowLayout());
                j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
                j.setVisible(true);    
    
                jlab=new JLabel("Here");
                j.add(jlab);
    
                JButton j1=new JButton("Button1");
                j1.setActionCommand("Your pressed Button 1");
    
                j1.addActionListener(this );
    
                j.add(j1);
    
                JButton j2=new JButton("Button2");
                j2.setActionCommand("Your pressed Button 2");
                j2.addActionListener(this );
                j.add(j2);
        }
        public void actionPerformed(ActionEvent Ae){
             jlab.setText(Ae.getActionCommand());
        }
    }
    
  • 或者,在main中创建同一类的对象,并将其传递给
    addActionListener
    方法。还要使JLabel为静态

        public class buttons1 implements ActionListener{
    
            JLabel jlab;
    
            buttons1(){
    
    
                    JFrame j = new JFrame("HP");
                    j.setSize(300,300);
                    j.setLayout(new FlowLayout());
                    j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
                    j.setVisible(true);    
    
                    jlab=new JLabel("Here");
                    j.add(jlab);
    
                    JButton j1=new JButton("Button1");
                    j1.setActionCommand("Your pressed Button 1");
    
                    j1.addActionListener(this );
    
                    j.add(j1);
    
                    JButton j2=new JButton("Button2");
                    j2.setActionCommand("Your pressed Button 2");
                    j2.addActionListener(this );
                    j.add(j2);
    
    
            }
    
            public void actionPerformed(ActionEvent Ae){
    
                      jlab.setText(Ae.getActionCommand());
    
            }
    
    
    
            public static void main(String args[]){
                new buttons1();
            } }  
    

  • main方法中的大部分内容应该移动到构造函数或非静态的
    init()
    方法中。这将解决眼前的问题-因为
    这个
    将存在。另请参阅
    
    public class buttons2 implements ActionListener{
        static JLabel jlab;
    
        public void actionPerformed(ActionEvent Ae){
    
                                 jlab.setText(Ae.getActionCommand());
    
                             }
    
    
    
        public static void main(String args[]){
    
            buttons2 s = new  buttons2();
    
            JFrame j = new JFrame("HP");
                j.setSize(300,300);
                j.setLayout(new FlowLayout());
                j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
                j.setVisible(true);    
    
                jlab=new JLabel("Here");
                j.add(jlab);
    
                JButton j1=new JButton("Button1");
                j1.setActionCommand("Your pressed Button 1");
    
                j1.addActionListener(s);
    
                j.add(j1);
    
                JButton j2=new JButton("Button2");
                j2.setActionCommand("Your pressed Button 2");
                j2.addActionListener(s);
                j.add(j2);
           }
       }