繁体   English   中英

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

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

我使用窗口构建器来创建前端GUI,我得到以下自动生成的代码。 在下面的代码中,我找不到为按钮单击事件实现ActionListener的语句。 它直接调用添加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在这里被称为匿名类

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

所以你的gui类不需要实现它。

窗口构建器始终创建匿名类(对于监听器),这样您就不需要在其他类或同一个类中实现监听器。 如果你不想使用匿名类,那么你必须手动修改代码。

暂无
暂无

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

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