import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyGUI extends JFrame implements ActionListener
{
JButton btnA;
public MyGUI()
{
setTitle("Test GUI");
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
setLocationRelativeTo(null);
JButton btnA=new JButton("A");
btnA.setSize(100, 20);
btnA.setLocation(0, 0);
btnA.addActionListener(this);
add(btnA);
}
public void actionPerformed(ActionEvent e)
{
System.out.println(e.toString());
System.out.println("Action detected");
if(e.getSource().equals(btnA))
System.out.println("A was pressed");
}
}
Run Code Online (Sandbox Code Playgroud)
我刚开始使用Java GUI.我有一个按钮.但是,当我单击该按钮时,第三个print语句不会执行,即使它应该是,从事件的toString()信息判断.它出什么问题了?