2013-02-11 38 views
0

我發現使用Java Swing框架在Java中實現了一個簡單的計算器。如何將此程序分爲幾個類

的源代碼是在這裏:

//Imports are listed in full to show what's being used 
//could just import javax.swing.* and java.awt.* etc.. 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.Container; 

public class SimpleCalc implements ActionListener{ 

    JFrame guiFrame; 
    JPanel buttonPanel; 
    JTextField numberCalc; 
    int calcOperation = 0; 
    int currentCalc; 

    //Note: Typically the main method will be in a 
    //separate class. As this is a simple one class 
    //example it's all in the one class. 
    public static void main(String[] args) { 

     //Use the event dispatch thread for Swing components 
     EventQueue.invokeLater(new Runnable() 
     { 

      @Override 
      public void run() 
      { 

       new SimpleCalc();   
      } 
     }); 

    } 

    public SimpleCalc() 
    { 
     guiFrame = new JFrame(); 

     //make sure the program exits when the frame closes 
     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("Simple Calculator"); 
     guiFrame.setSize(300,300); 

     //This will center the JFrame in the middle of the screen 
     guiFrame.setLocationRelativeTo(null); 

     numberCalc = new JTextField(); 
     numberCalc.setHorizontalAlignment(JTextField.RIGHT); 
     numberCalc.setEditable(false); 

     guiFrame.add(numberCalc, BorderLayout.NORTH); 

     buttonPanel = new JPanel(); 

     //Make a Grid that has three rows and four columns 
     buttonPanel.setLayout(new GridLayout(4,3)); 
     guiFrame.add(buttonPanel, BorderLayout.CENTER); 

     //Add the number buttons 
     for (int i=1;i<10;i++) 
     { 
      addButton(buttonPanel, String.valueOf(i)); 
     } 

     JButton addButton = new JButton("+"); 
     addButton.setActionCommand("+"); 

     OperatorAction subAction = new OperatorAction(1); 
     addButton.addActionListener(subAction); 

     JButton subButton = new JButton("-"); 
     subButton.setActionCommand("-"); 

     OperatorAction addAction = new OperatorAction(2); 
     subButton.addActionListener(addAction); 

     JButton equalsButton = new JButton("="); 
     equalsButton.setActionCommand("="); 
     equalsButton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent event) 
      { 
       if (!numberCalc.getText().isEmpty()) 
       { 
        int number = Integer.parseInt(numberCalc.getText()); 
        if (calcOperation == 1) 
        { 
         int calculate = currentCalc + number; 
         numberCalc.setText(Integer.toString(calculate)); 
        } 
        else if (calcOperation == 2) 
        { 
         int calculate = currentCalc - number; 
         numberCalc.setText(Integer.toString(calculate)); 
        } 
       } 
      } 
     }); 

     buttonPanel.add(addButton); 
     buttonPanel.add(subButton); 
     buttonPanel.add(equalsButton); 
     guiFrame.setVisible(true); 
    } 

    //All the buttons are following the same pattern 
    //so create them all in one place. 
    private void addButton(Container parent, String name) 
    { 
     JButton but = new JButton(name); 
     but.setActionCommand(name); 
     but.addActionListener(this); 
     parent.add(but); 
    } 

    //As all the buttons are doing the same thing it's 
    //easier to make the class implement the ActionListener 
    //interface and control the button clicks from one place 
    @Override 
    public void actionPerformed(ActionEvent event) 
    { 
     //get the Action Command text from the button 
     String action = event.getActionCommand(); 

     //set the text using the Action Command text 
     numberCalc.setText(action);  
    } 

    private class OperatorAction implements ActionListener 
    { 
     private int operator; 

     public OperatorAction(int operation) 
     { 
      operator = operation; 
     } 

     public void actionPerformed(ActionEvent event) 
     { 
      currentCalc = Integer.parseInt(numberCalc.getText()); 
      calcOperation = operator; 
     } 
    } 
} 

我願把OperatorAction類成一個單獨的類,我都試過了,但再有就是從JTextField中獲取文本的一個問題,因爲這在新班級中不可用。那麼,如何做到這一點?

+0

這很簡單,只需將'JTextField'設置爲'OperatorAction'類的一個字段即可。 – mre 2013-02-11 16:47:54

回答

2

所有你需要的是另一個構造函數的參數(和匹配的實例字段):

class OperatorAction implements ActionListener 
{ 
    private int operator; 
    private SimpleCalc calc; 

    public OperatorAction(SimpleCalc c, int operation) 
    { 
     calc = c; 
     operator = operation; 
    } 

    public void actionPerformed(ActionEvent event) { 
     calc.setCurrentCalc(Integer.parseInt( 
      ((JTextField)event.getSource()).getText())); 
     calcOperation = operator; 
    } 
} 

和揭露通過一個setter的SimpleCalc#currentCalc財產。

雖然這不是很乾淨的設計,因爲您仍然有OperatorActionSimpleCalc之間的緊密耦合,但它可能是您的開始。

+0

+1,或提供增變器方法。 – mre 2013-02-11 16:50:02

-1

看起來您需要使用模型視圖控制器(MVC)模式! MVC是許多語言和系統不可或缺的部分,所以學習非常重要!你想要做的是將程序分成3層,* M * odels,* V * see,* C * ontrollers(在這種情況下,該項目可能很簡單)

模型將存儲您的數據,這些可以是幾個類或整個數據庫,具體取決於您的項目的大小。

瀏覽次數即將顯示您的數據。

控制器將管理其餘的。

所以你在這裏做的是有一個模型只存儲輸入到計算器,一個控制器接受輸入,做實際的數學運算,並將輸出推送到視圖。

相關問題