Java 多按钮Swing的ActionListener
我想使用相同的EventHandler处理多个按钮的操作。单击按钮时,标签的文本将相应更改 此代码显示了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
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());
}
}
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);
}
}