实施actionevent方法

时间:2014-01-22 07:37:08

标签: java swing jbutton actionlistener calculator

用于实现动作事件方法,以便在摇摆中发生jbuttons。这些是我的按钮。现在我想让这些按钮动作..请帮助我

    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button_plus = new JButton("+");

    JButton button4 = new JButton("4");
    JButton button5 = new JButton("5");
    JButton button6 = new JButton("6");
    JButton button_minus = new JButton("-");

    JButton button7 = new JButton("7");
    JButton button8 = new JButton("8");
    JButton button9 = new JButton("9");
    JButton button_multiple = new JButton("*");

    JButton button0 = new JButton("0");
    JButton clear = new JButton("clear");
    JButton delete = new JButton("delete");
    JButton button_divide = new JButton("/");
    JButton button_equals = new JButton("=");

1 个答案:

答案 0 :(得分:1)

public class yourClass extends JFrame implements ActionListener

之后:

button1.addActionListener(this);
// repeat for every button

然后实现ActionPerformed方法

public void actionPerformed(ActionEvent e){

     Object obj = e.getSource();
     if (obj == button1){
         // Your Code
     }//else if ....
}