2

There is an issue with the code where the ActionListener will not pick up on any action when i call upon it with the buttons. I have done this before but this is my first time attempting a GridBagLayout and it doesn't seem to be working correctly. The GUI is fully functional. What could I do to get the ActionListener to work? Here is the entire class.

package body;

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class mainFrame extends JFrame implements ActionListener{

    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    static String string = "default";

    static JButton one, two, three, four, five, six, seven, eight, nine, zero;
    static JButton plus, minus, multiply, divide, equals;

    public void init() {
        one.setActionCommand("one");
        one.addActionListener(this);

        two.setActionCommand("two");
        two.addActionListener(this);

        three.setActionCommand("three");
        three.addActionListener(this);

        four.setActionCommand("four");
        four.addActionListener(this);

        five.setActionCommand("five");
        five.addActionListener(this);

        six.setActionCommand("six");
        six.addActionListener(this);

        seven.setActionCommand("seven");
        seven.addActionListener(this);

        eight.setActionCommand("eight");
        eight.addActionListener(this);

        nine.setActionCommand("nine");
        nine.addActionListener(this);

        zero.setActionCommand("zero");
        zero.addActionListener(this);

        plus.setActionCommand("plus");
        plus.addActionListener(this);

        minus.setActionCommand("minus");
        minus.addActionListener(this);

        multiply.setActionCommand("multiply");
        multiply.addActionListener(this);

        divide.setActionCommand("divide");
        divide.addActionListener(this);

        equals.setActionCommand("equals");
        equals.addActionListener(this);

    }

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

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

        if (shouldFill) {
            c.fill = GridBagConstraints.HORIZONTAL;
        }

        if (shouldWeightX) {
            c.weightx = 0.5;
        }

        JLabel display = new JLabel(string);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(display, c);

        one = new JButton("1");
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(one, c);

        two = new JButton("2");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 1;
        pane.add(two, c);

        three = new JButton("3");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 1;
        pane.add(three, c);

        plus = new JButton("+");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 1;
        pane.add(plus, c);

        four = new JButton("4");
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(four, c);

        five = new JButton("5");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 2;
        pane.add(five, c);

        six = new JButton("6");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        pane.add(six, c);

        minus = new JButton("-");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 2;
        pane.add(minus, c);

        seven = new JButton("7");
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        pane.add(seven, c);

        eight = new JButton("8");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 3;
        pane.add(eight, c);

        nine = new JButton("9");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 3;
        pane.add(nine, c);

        multiply = new JButton("*");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 3;
        pane.add(multiply, c);

        zero = new JButton("0");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 4;
        pane.add(zero, c);

        equals = new JButton("Equals");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridwidth = 2;
        c.gridy = 4;
        pane.add(equals, c);

        divide = new JButton("/");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.PAGE_END; 
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 4;
        pane.add(divide, c);  
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);

        addComponentsToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "one") {
            System.out.println("hello");
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

1

Besides the == and equals I pointed out, you never call init(), which initializes your action commands.

Also note, you class is already a JFrame. Either use the class JFrame or use the instance JFrame.

You should fix the above problem so that you can properly call your init() method.

Try this

private static void createAndShowGUI() {
    mainFrame frame = new mainFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    addComponentsToPane(frame.getContentPane());
    frame.init();            <<<<<================== Don't forget about meeeee!
    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if ("one".equals(e.getActionCommand())) {
        System.out.println("hello");
    }
}

Also you should be following Java naming convention. Class names begin with capital letters. So mainFrameMainFrame

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720