如何给多个按钮不同的命令

时间:2015-12-01 20:41:52

标签: java swing events jbutton actionlistener

我一直在尝试用Javax摆动计算器。我知道如何使一个按钮有一个命令,但我不知道如何在Java中给更多按钮更多的命令?我想给按钮一个清晰的动作。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Calculator extends JFrame {

    private JButton a0;
    private JButton a1;
    private JButton a2;
    private JButton a3;
    private JButton a4;
    private JButton a5;
    private JButton a6;
    private JButton a7;
    private JButton a8;
    private JButton a9;
    private JButton clear;
    private JButton plusminus;
    private JButton plus;
    private JButton minus;
    private JButton multiply;
    private JButton divide;
    private JButton equal;
    private JButton decimal;

    private JLabel resultLabel;

    public Calculator() {
        setLayout(new FlowLayout());

        a0 = new JButton("0");
        add(a0);

        a1 = new JButton("1");
        add(a1);

        a2 = new JButton("2");
        add(a2);

        a3 = new JButton("3");
        add(a3);

        a4 = new JButton("4");
        add(a4);

        a5 = new JButton("5");
        add(a5);

        a6 = new JButton("6");
        add(a6);

        a7 = new JButton("7");
        add(a7);

        a8 = new JButton("8");
        add(a8);

        a9 = new JButton("9");
        add(a9);

        decimal = new JButton(".");
        add(decimal);

        clear = new JButton("C");
        add(clear);

        plusminus = new JButton("+/-");
        add(plusminus);

        plus = new JButton("+");
        add(plus);

        minus = new JButton("-");
        add(minus);

        multiply = new JButton("X");
        add(multiply);

        divide = new JButton("/");
        add(divide);

        equal = new JButton("=");
        add(equal);

        resultLabel = new JLabel("");
        add(resultLabel);

    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            a0.addActionListener(this);

            a1.addActionListener(this);

            a2.addActionListener(this);

            a3.addActionListener(this);

            a4.addActionListener(this);

            a5.addActionListener(this);

            a6.addActionListener(this);

            a7.addActionListener(this);

            a8.addActionListener(this);

            a9.addActionListener(this);

            clear.addActionListener(this);

            decimal.addActionListener(this);

            plusminus.addActionListener(this);

            plus.addActionListener(this);

            minus.addActionListener(this);

            multiply.addActionListener(this);

            divide.addActionListener(this);

            equal.addActionListener(this);

            if (event.getSource() == a1) {
                resultLabel.setText("0");
            }

        }
    }

    public static void main(String[] args) {
        Calculator gui = new Calculator();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 800);
        gui.setVisible(true);
        gui.setTitle("Caclculator");

    }
}

2 个答案:

答案 0 :(得分:1)

我将向您展示1个按钮,为所有其他按钮执行相同操作,

public class Calculator extends JFrame  implements ActionListener {

    JButton a0 = new JButton("0");
    a0.addActionListener(this);
    add(a0); 

    public void actionPerformed(ActionEvent event) {

        String command = e.getActionCommand();

        switch(command){

            case "0":
                    //do whatever want while button '0' press
                    break;
            case "1":
                    //do whatever want while button '1' press
                    break;
            case "2":
                    //do whatever want while button '2' press
                    break;      


        }

    }
}

答案 1 :(得分:1)

您将创建一个实现 ActionListener 的类。

然后你创建这个类的实例,然后将它作为ActionListener添加到每个按钮(代码如下):

@SuppressWarnings("serial")
public class Calculator extends JFrame {

// array with the number 0-9
private JButton[] numbers = new JButton[9];
private JButton clear;
private JButton plusminus;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton decimal;

private JLabel resultLabel = new JLabel("");

public Calculator() {

    //
    setSize(300, 300);
    setLayout(new FlowLayout());

    // The Class which implements the ActionListener
    EventListener listener = new EventListener();

    // Create each button from 0-9 here
    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = new JButton(i + "");
        numbers[i].addActionListener(listener);
        add(numbers[i]);
    }

    // Create the other Buttons
    decimal = new JButton(".");
    add(decimal);

    clear = new JButton("C");
    add(clear);

    plusminus = new JButton("+/-");
    add(plusminus);

    plus = new JButton("+");
    add(plus);

    minus = new JButton("-");
    add(minus);

    multiply = new JButton("X");
    add(multiply);

    divide = new JButton("/");
    add(divide);

    equal = new JButton("=");
    add(equal);

    clear.addActionListener(listener);

    decimal.addActionListener(listener);

    plusminus.addActionListener(listener);

    plus.addActionListener(listener);

    minus.addActionListener(listener);

    multiply.addActionListener(listener);

    divide.addActionListener(listener);

    equal.addActionListener(listener);

    add(resultLabel);
}

public static void main(String[] args) {
    Calculator gui = new Calculator();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(300, 800);
    gui.setVisible(true);
    gui.setTitle("Caclculator");

}

// TODO EventListener
 class EventListener implements ActionListener {

    public void actionPerformed(ActionEvent event) {
        Object v = event.getSource();

        if (v == clear) {
            // do something..
        } else if (v == decimal) {
            // do something...
        } else if (v == plusminus) {
            // do something...
        }
        // etc continue this.....

    }
}

}