2015-11-19 52 views
0

今天我寫了一個圖形計算器,它只通過按鈕來操作,在完成後我意識到我沒有在整個代碼中定義一個主要方法。我一直在想這個問題一段時間,但用我有限的知識卻無法想出解決這個問題的方法。Java Swing Calculator - 缺乏主要方法

我正在考慮用main函數替換我用來構造計算器的方法,但遇到了訪問main之外的非靜態變量的問題。

我參考代碼:

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

public class GraphicalCalculator{ 

    // Hold first and second numbers, to be acted upon 
    private double num1 = 0; 
    private double num2 = 0; 
    private char function = ' '; 

    // User's 'screen' 
    private JTextField Scr; 

    public GraphicalCalculator(){ 

     // Numerical Buttons (0-9) 
     JButton[] bNum = new JButton[10]; 
     for(int i = 9; i >= 0; i--){ 
      bNum[i] = new JButton(Integer.toString(i)); 
     } 

     // Function buttons 
     JButton bClear = new JButton("Clear"); 
     JButton bCalc = new JButton("Calculate"); 
     JButton bAdd = new JButton("+"); 
     JButton bSub = new JButton("-"); 
     JButton bMul = new JButton("*"); 
     JButton bDiv = new JButton("/"); 

     // TextField which acts as user's screen 
     Scr = new JTextField(""); 
     Scr.setEnabled(false); 

     // Calculator frame, contains entire GUI 
     JFrame frame = new JFrame("Calculator"); 
      frame.setSize(300, 400); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 

     // Main pane, contains 3 sub-panes 
     JPanel pane = (JPanel) frame.getContentPane(); 
      pane.setLayout(new GridLayout(3, 1)); 

     // Add numerical buttons to Panel 
     JPanel numButtons = new JPanel(); 
      numButtons.setLayout(new GridLayout(4, 3)); 
      for(int i = 9; i >= 0; i--){ 
       numButtons.add(bNum[i]); 
      } 

     // Add function buttons to Panel 
     JPanel fncButtons = new JPanel(); 
      fncButtons.setLayout(new GridLayout(3, 2)); 
      fncButtons.add(bAdd); 
      fncButtons.add(bSub); 
      fncButtons.add(bMul); 
      fncButtons.add(bDiv); 
      fncButtons.add(bClear); 
      fncButtons.add(bCalc); 

     // Implementing action listeners for each numerical button 
     bNumAction[] bNumActions = new bNumAction[10]; 
     for(int i = 0; i <= 9; i++){ 
      bNumActions[i] = new bNumAction(bNum[i]); 
      bNum[i].addActionListener(bNumActions[i]); 
     } 

     // Implementing action listeners for each function button 
     bAdd add = new bAdd(); 
      bAdd.addActionListener(add);  
     bSub sub = new bSub(); 
      bSub.addActionListener(sub);   
     bMul mul = new bMul(); 
      bMul.addActionListener(mul);   
     bDiv div = new bDiv(); 
      bDiv.addActionListener(div);   
     bClear clear = new bClear(); 
      bClear.addActionListener(clear);    
     bCalc calc = new bCalc(); 
      bCalc.addActionListener(calc); 

     pane.add(Scr); 
     pane.add(numButtons); 
     pane.add(fncButtons); 
     frame.add(pane); 
    } 

    private class bNumAction implements ActionListener{ 

     // Holds value of button pressed 
     private String temp; 

     // Retrieve value of button pressed 
     public bNumAction(JButton pressed){ 
      this.temp = pressed.getText(); 
     } 

     // If there is currently a value on the screen, add it it, otherwise set screen value to input 
     public void actionPerformed(ActionEvent e){ 
      if(!Scr.getText().equals("")){ 
       Scr.setText(Scr.getText() + temp); 
      } else { 
       Scr.setText(""); 
       actionPerformed(e); 
      } 
     } 
    } 

    // Following 4 classes transmit function key pressed to numIn 
    private class bAdd implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      numIn('+'); 
     } 
    } 

    private class bSub implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      numIn('-'); 
     } 
    } 

    private class bMul implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      numIn('*'); 
     } 
    } 

    private class bDiv implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      numIn('/'); 
     } 
    } 

    // Stores value of input number and function used, then clears for next input 
    public void numIn (char fnc){ 
     if(num1 == 0){ 
      num1 = Double.parseDouble(Scr.getText()); 
      Scr.setText(""); 
     } else { 
      num2 = Double.parseDouble(Scr.getText()); 
      Scr.setText(""); 
     } 
     function = fnc; 
    } 

    // Clears all values if Clear button is pressed 
    private class bClear implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      Scr.setText(""); 
      num1 = 0; 
      num2 = 0; 
      function = ' '; 
     } 
    } 

    // Calculates result when Calculate button is pressed, stores result as num1 for use in continued calculation 
    private class bCalc implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      num2 = Double.parseDouble(Scr.getText()); 
      if(function == '+'){ 
       Scr.setText(Double.toString((Math.round((num1/num2) * 100))/100)); 
      } else if(function == '-'){ 
       Scr.setText(Double.toString(num1 - num2)); 
      } else if(function == '*'){ 
       Scr.setText(Double.toString(num1 * num2)); 
      } else if(function == '/'){ 
       Scr.setText(Double.toString(num1/num2)); 
      } else { 
        Scr.setText(String.valueOf(num1)); 
      } 
      num1 = Double.parseDouble(Scr.getText()); 
     } 
    } 
} 

尋求解決方法的任何解決方案或建議,將不勝感激。

回答

3

您的構造函數似乎在做所有設置GUI的工作。因此,您所需要的只是創建您的課程實例:

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new GraphicalCalculator(); 
     } 
    }); 
} 
+0

感謝您的快速和輕鬆響應! – Skad

+1

儘管它應該被封裝在Event Dispatch Thread中。 –

+0

@Zek:很可能;我從來沒有做過Java Swing這麼久 - 隨時編輯答案或添加自己的答案。 –