Java 如何将actionListener添加到按钮?
我正在从头开始制作一个新程序,在程序中添加一个动作监听器,这让我很困惑。这个程序只是为了实验,视觉界面不是很令人愉快。现在就不管它吧 我试过但是。addActionListener(这个); 但它仍然给了我一个错误 ./Inventory.java:31:error:非静态变量无法从静态上下文引用此变量Java 如何将actionListener添加到按钮?,java,Java,我正在从头开始制作一个新程序,在程序中添加一个动作监听器,这让我很困惑。这个程序只是为了实验,视觉界面不是很令人愉快。现在就不管它吧 我试过但是。addActionListener(这个); 但它仍然给了我一个错误 ./Inventory.java:31:error:非静态变量无法从静态上下文引用此变量 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public cla
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Inventory extends JFrame implements ActionListener {
public Inventory() {
startgui();
}
public static void startgui() {
JFrame frame = new JFrame("Inventory");
frame.setLayout(null);
frame.setSize(500, 480);
frame.setLocation(540, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JLabel label = new JLabel("Welcome To My Program");
label.setBounds(10, 10, 310, 60);
label.setVisible(true);
frame.add(label);
JButton but = new JButton("Well Played");
but.setBounds(20, 20, 70, 30);
frame.add(but);
but.addActionListener(/* what do I insert here? */);
}
public void actionPerformed(ActionEvent e) {
System.out.print("yess");
}
}
不能在静态方法中使用
this
关键字。这是因为This
关键字引用运行该方法的类的当前实例,但静态方法不属于单个实例,而是在所有实例之间共享
您可以阅读更多有关此
关键字的信息。您还可以阅读有关static关键字的内容。要解决您的问题,请尝试:
but.addActionListener(frame);
只有将frame
更改为Inventory
的实例时,这才有效,但在更改时要谨慎,因为这样可以创建无限循环
一般来说,我认为您不需要构造一个新的
Frame
对象,而是将startgui
设置为非静态,并使用this
关键字。Saviour解释得很好,我将为您的代码添加更多内容。因为您实现了
ActionListener
,并且希望在同一类中向按钮添加功能,所以需要但是.addActionListener(this)
<代码>此引用类的一个instance。但是,因为您有一个静态方法,并且在方法内部有这个
关键字,所以编译器会说“否”。您可以删除静态关键字并添加此
关键字:
public class Inventory extends JFrame implements ActionListener{
public Inventory(){
startgui();
}
public void startgui(){ //<---Here delete static
JFrame frame = new JFrame("Inventory");
//Code...
but.addActionListener(this); //<---Here add this
}
public void actionPerformed(ActionEvent e) {
System.out.print("yess");
}
}
然后,只需创建一个匿名对象:newbuttonlistener()
,而不是this
。
就我个人而言,我会删除这个静态关键字,并使另一个类实现ActionListener,这将删除类的复杂性,您的代码行将更少,而且还会增加可读性
此外,尝试使用布局管理器
。Swing必须与布局一起使用。你可以在没有布局的情况下创建一个应用程序,但是你会发现后面的问题告诉你“你必须使用布局”
最后,由于您扩展了JFrame
,因此不再需要JFrame
实例,因为您的类已经是JFrame
(从继承中):
非常感谢,所有这些评论都会很有帮助。有这样一个社区真好。继续努力,伙计们!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("HII");
}
}
public class ApplicationPoint extends JFrame{
JLabel label;
JButton but;
public ApplicationPoint(String title){
super(title);
startgui();
setVisible(true);
}
public void startgui(){
setLayout(new FlowLayout());
setSize(500,480); //I'm not using frame.setSize, just simply setSize(500,480)
setLocationRelativeTo(null); //This will display the frame in the middle of your screen
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Welcome To My Program");
label.setVisible(true);
but = new JButton("Well Played");
add(but);
add(label);
but.addActionListener(new ButtonListener());
}
public void actionPerformed(ActionEvent e) {
System.out.print("yess");
}
public static void main(String []args) {
new ApplicationPoint("Inventory");
}
}