2015-11-19 38 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这么久 - 随时编辑答案或添加自己的答案。 –