繁体   English   中英

无法在 gridbaglayout 中添加动作侦听器

[英]Unable to add action listener in gridbaglayout

这是我的代码,如何添加动作侦听器我已经尝试过我知道的方法,但由于静态方法,我无法添加它。Netbeans IDE 建议在程序中使用动作侦听器,但我无法使用它,有人可以建议更多声明动作侦听器的简单方法。

公共类计算{

public static void addComponentsToPane(Container pane) {


        pane.setLayout(new GridBagLayout());
        GridBagConstraints gBC = new GridBagConstraints();

        gBC.ipady = 40;
        gBC.ipadx = 40;

        JTextField JTextField = new JTextField("Hello");
        gBC.fill = GridBagConstraints.HORIZONTAL;
        gBC.gridx = 0;
        gBC.gridy = 0;
        gBC.gridwidth = 4;
        JTextField.setEditable(false);
        pane.add(JTextField, gBC);

        //JButton jbnButton;
        gBC.gridwidth = 1;

        JButton b7 = new JButton("7");
        gBC.gridx = 0;
        gBC.gridy = 1;
        pane.add(b7, gBC);
        // more calculator buttons declared here

//这里是如何建议actionlistner的

        b0.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b0ActionPerformed(evt);
        }

        private void b0ActionPerformed(ActionEvent evt)  }
        `
    });
    b0.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        b0ActionPerformed(evt);
    }
    private void b0ActionPerformed(Actionenter Event evt) {}
    });}

    private static void createAndShowGUI() {}

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

我为你试试这个。 您可以在 addComponentsToPane 方法中为您声明的每个组件添加动作侦听器。 此外,您忘记传递框架,因此您可以像我一样在 main 方法中传递它。

public class Calc {

public static void addComponentsToPane(Container pane) {

    pane.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();

    gBC.ipady = 40;
    gBC.ipadx = 40;

    JTextField JTextField = new JTextField("Hello");
    gBC.fill = GridBagConstraints.HORIZONTAL;
    gBC.gridx = 0;
    gBC.gridy = 0;
    gBC.gridwidth = 4;
    JTextField.setEditable(false);
    pane.add(JTextField, gBC);

    //JButton jbnButton;
    gBC.gridwidth = 1;

    JButton b7 = new JButton("7");
    gBC.gridx = 0;
    gBC.gridy = 1;
    pane.add(b7, gBC);
    // more calculator buttons declared here

    b7.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Write your action here

        }
    });

}

private static void createAndShowGUI(JFrame pane) {
    addComponentsToPane(pane);
    pane.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(new JFrame());
        }
    });
}

}

暂无
暂无

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

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