我已经用Java创建了简单的GUI来测试单击按钮的工作。但是当我按下按钮时,按钮不起作用。
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class rigotechnology implements ActionListener{
JFrame test;
JButton Try;
public void main(){
JFrame test = new JFrame("TEST FILE");
test.setLayout(null);
JButton Try = new JButton("TRY");
Try.setBounds(50,50,80,80);
test.add(Try);
test.setSize(200,200);
test.setVisible(true);
Try.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Try){
JOptionPane.showMessageDialog(test,"hello");
}
}
public static void main(String args[]){
rigotechnology o = new rigotechnology();
o.main();
}
}
答案 0 :(得分:0)
在方法main()中,您重新定义了本地JButton Try,因此try in actionPerformed始终为null。
public void main(){
test = new JFrame("TEST FILE");
test.setLayout(null);
Try = new JButton("TRY");
Try.setBounds(50,50,80,80);
test.add(Try);
test.setSize(200,200);
test.setVisible(true);
Try.addActionListener(this);
}