2015-12-01 22 views
1

我一直在試圖用Javax swing來製作一個計算器。我知道如何讓一個按鈕有一個命令,但我不知道如何在Java中給更多按鈕更多的命令?我想給按鈕a0-清除動作。如何給多個按鈕不同的命令

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

    } 
} 
+0

也許你需要爲每個按鈕不同的動作偵聽器,或有動作監聽檢查按鈕開展活動。 – markspace

+0

您正在將'ActionListener'添加到'actionPerformed'方法中的按鈕中。您應該在您的構造函數中完成此操作。對於你的問題,你在正確的軌道上 - 你需要確定哪個按鈕被按下條件語句:'if(event.getSource()== a1)' –

+0

看看[如何使用動作]( http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) – MadProgrammer

回答

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

您將創建一個實現的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..... 

    } 
} 

}