JFrame中的ActionListener

时间:2014-04-11 18:07:42

标签: java swing static actionlistener gridbaglayout

当我使用按钮调用任何操作时,ActionListener将无法接收代码时出现问题。我以前做过这个,但这是我第一次尝试GridBagLayout并且它似乎没有正常工作。 GUI功能齐全。我该怎么做让ActionListener起作用?这是整个班级。

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

1 个答案:

答案 0 :(得分:1)

除了我指出的==equals之外,您永远不会调用init()来初始化您的操作命令。

另请注意,您的课程已经是JFrame。使用类JFrame或使用实例JFrame

您应该解决上述问题,以便正确调用init()方法。

试试这个

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

此外,您应该遵循Java命名约定。班级名称以大写字母开头。所以mainFrameMainFrame