简体   繁体   中英

Unable to add action listener in gridbaglayout

This is my code how can I add action listener I already tried methods I known but due to static methods I am unable to add it.Netbeans IDE suggested the action listener in the program but I am not able to use it can someone suggest a more simple method to declare the action listener.

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

//Here how it suggest the 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();
            }
        });
    }

}

I try this for you. You can add action listener in addComponentsToPane method for every component you declare. Also, you forgot to pass the frame so you can pass it in the main method as I do.

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());
        }
    });
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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