Java Swing Calculator - 缺少主要方法

时间:2015-11-19 00:06:14

标签: java swing

今天我写了一个图形计算器,它只通过按钮操作,完成后我意识到我没有在我的代码中定义一个主方法。我已经考虑了一段时间,但凭借我有限的知识,我想出了解决这个问题的方法。

我正在考虑用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());
        }
    }
}

非常感谢任何有关变通方法的解决方案或建议。

1 个答案:

答案 0 :(得分:3)

您的构造函数似乎正在完成设置GUI的所有工作。因此,您只需要创建类的实例:

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