Java 为什么在eclipse中使用window builder创建GUI时不实现ActionListener?
我使用WindowBuilder创建了一个前端GUI,我得到了以下自动生成的代码。在下面的代码中,我找不到为按钮单击事件实现ActionListener的语句。它直接调用addActionListener和actionPerformed,而不使用语句公共类gui扩展JFrame,实现ActionListener,正如我在教程中所学到的那样Java 为什么在eclipse中使用window builder创建GUI时不实现ActionListener?,java,swing,actionlistener,Java,Swing,Actionlistener,我使用WindowBuilder创建了一个前端GUI,我得到了以下自动生成的代码。在下面的代码中,我找不到为按钮单击事件实现ActionListener的语句。它直接调用addActionListener和actionPerformed,而不使用语句公共类gui扩展JFrame,实现ActionListener,正如我在教程中所学到的那样 public class gui extends JFrame { private JPanel contentPane; /**
public class gui extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gui frame = new gui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public gui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 386, 451);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
ActionListener
在此处被称为
所以您的
gui
类不需要实现它。窗口生成器始终创建匿名类(用于侦听器),这样您就不需要在其他类或同一类中实现侦听器。如果不想使用匿名类,则必须手动修改代码
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});