简体   繁体   English

在eclipse中使用窗口构建器创建GUI时,为什么没有实现ActionListener?

[英]why ActionListener is not implemented when creating GUI using window builder in eclipse?

Im using window builder to create a front end GUI and I get the following automatically generated code. 我使用窗口构建器来创建前端GUI,我得到以下自动生成的代码。 In the below code I cannot find the statement that implements ActionListener for the button click event. 在下面的代码中,我找不到为按钮单击事件实现ActionListener的语句。 Its directly calling add addActionListener and actionPerformed without the statement Public class gui extends JFrame implements ActionListener as I learned in the tutorials. 它直接调用添加addActionListener和actionPerformed而没有语句Public class gui extends JFrame实现了ActionListener,正如我在教程中学到的那样。

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 is called as an anonymous class here: ActionListener在这里被称为匿名类

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });

so your gui class doesn't need to implement it. 所以你的gui类不需要实现它。

window builder always create anonymous class (for Listeners) so that you don't need to implements Listener in other class or in same class. 窗口构建器始终创建匿名类(对于监听器),这样您就不需要在其他类或同一个类中实现监听器。 if you dont want to use anonymous class then you have to modify the code manually. 如果你不想使用匿名类,那么你必须手动修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM