GUI计算器计算

时间:2014-04-17 18:59:07

标签: java user-interface actionlistener

我目前正在处理一些我正在研究的GUI计算器的问题。计算器编译并进行计算。要进行计算,您必须先输入一个数字,选择一个函数(+, - ,*或/),选择另一个数字,然后按等号。在执行等式后,必须先清除程序,然后再继续。我无法弄清楚如何制作它,以便程序在继续计算之前不需要按清除。我希望能够单击一个数字,单击+,单击一个数字,单击等于获得答案,然后再次按+以在等式中添加另一个数字。对此的任何帮助将不胜感激。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class GUICalc extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JButton[] numberButtons;
    private JButton[] upButtons;

    private JTextField field;
    private double num2, ans; 
    private int op;

    // 0 = gridx 1 gridy 2 gridwidth 3 gridheight
    private int[][] numConstraints = new int[][] {
            {0, 4, 1, 1},
            {0, 1, 1, 1},
            {1, 1, 1, 1},
            {2, 1, 1, 1},
            {0, 2, 1, 1},
            {1, 2, 1, 1},
            {2, 2, 1, 1},
            {0, 3, 1, 1},
            {1, 3, 1, 1},
            {2, 3, 1, 1},
    };

    private int[][] upConstraints = new int[][] {
            {1, 4, 1, 1},
            {3, 4, 1, 1},
            {3, 3, 1, 1},
            {3, 2, 1, 1},
            {3, 1, 1, 1},
            {0, 5, 4, 1},
            {2, 4, 1, 1},
    };

    public GUICalc() {
        setPreferredSize(new Dimension(666, 666)); //width & heigth

        GridBagLayout layout; // used to be private
        GridBagConstraints gbc; // used to be private

        layout = new GridBagLayout();
        layout.columnWidths = new int[] {60, 60, 60, 60};
        layout.rowHeights = new int[] {60, 60, 60, 60, 60, 60};
        setLayout(layout);

        gbc = new GridBagConstraints();

            numberButtons = new JButton[10];

            numberButtons[0] = new JButton("0");
            numberButtons[1] = new JButton("1");
            numberButtons[2] = new JButton("2");
            numberButtons[3] = new JButton("3");
            numberButtons[4] = new JButton("4");
            numberButtons[5] = new JButton("5");
            numberButtons[6] = new JButton("6");
            numberButtons[7] = new JButton("7");
            numberButtons[8] = new JButton("8");
            numberButtons[9] = new JButton("9");

            for(int i = 0; i < numberButtons.length; i++){
            gbc.gridx = numConstraints[i][0];
            gbc.gridy = numConstraints[i][1];
            gbc.gridwidth = numConstraints[i][2];
            gbc.gridheight = numConstraints[1][3];
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(2, 2, 2, 2); 
            numberButtons[i].addActionListener(this);
            add(numberButtons[i], gbc);
            }

        upButtons = new JButton[7];

        upButtons[0] = new JButton(".");
        upButtons[1] = new JButton("/");
        upButtons[2] = new JButton("*");
        upButtons[3] = new JButton("-");
        upButtons[4] = new JButton("+");
        upButtons[5] = new JButton("=");
        upButtons[6] = new JButton("C");

        for(int i = 0; i < upButtons.length; i++){
            gbc.gridx = upConstraints[i][0];
            gbc.gridy = upConstraints[i][1];
            gbc.gridwidth = upConstraints[i][2];
            gbc.gridheight = upConstraints[1][3];
            gbc.insets = new Insets(2, 2, 2, 2);
            upButtons[i].addActionListener(this);
            add(upButtons[i], gbc); 


        }

        field = new JTextField();
        field.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        field.setEditable(false);
        field.setFont(new Font("Arial",Font.PLAIN, 24));
        field.setText(null);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;

        add(field, gbc);

    }


    public static void main(String [] args){
        JFrame frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(new BorderLayout());
        frame.add(new GUICalc(), BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);     

    }


    @Override
    public void actionPerformed(ActionEvent e) {

            if(e.getSource() == numberButtons[0]){
            field.setText(field.getText() + 0);
            }
            if(e.getSource() == numberButtons[1]){
            field.setText(field.getText() + 1);
            }
            if(e.getSource() == numberButtons[2]){
            field.setText(field.getText() + 2);
            }
            if(e.getSource() == numberButtons[3]){
            field.setText(field.getText() + 3);
            }
            if(e.getSource() == numberButtons[4]){
            field.setText(field.getText() + 4);
            }
            if(e.getSource() == numberButtons[5]){
            field.setText(field.getText() + 5);
            }
            if(e.getSource() == numberButtons[6]){
            field.setText(field.getText() + 6);
            }
            if(e.getSource() == numberButtons[7]){
            field.setText(field.getText() + 7);
            }
            if(e.getSource() == numberButtons[8]){
            field.setText(field.getText() + 8);
            }
            if(e.getSource() == numberButtons[9]){
            field.setText(field.getText() + 9);
            }

        if(e.getSource() == upButtons[0] && !field.getText().contains(".")) {
            field.setText(field.getText() + ".");
        }

        if(e.getSource() == upButtons[1]) {
            ans = Integer.parseInt(field.getText());
            op = 4;
            field.setText("");
        }

        if(e.getSource() == upButtons[2]) {
            ans = Integer.parseInt(field.getText());
            op = 3;
            field.setText("");
        }
        if(e.getSource() == upButtons[3]) {
            ans = Integer.parseInt(field.getText());
            op = 2;
            field.setText("");
        }
        if(e.getSource() == upButtons[4]) {
            ans = Integer.parseInt(field.getText());
            op = 1;
            field.setText("");
        }
        if(e.getSource() == upButtons[5]) {
            num2 = Integer.parseInt(field.getText());

            if(op == 1){
                ans = ans + num2;
            } else if(op == 2){
                ans = ans - num2;

            } else if(op == 3){
                ans = ans * num2;
            } else if(op == 4){
                ans = ans / num2;
            } 

            op = 0;
            field.setText("" + ans);
        }

        if(e.getSource() == upButtons[6]) {
            ans = 0.0;
            field.setText("");
        }
    }


}

2 个答案:

答案 0 :(得分:1)

您的程序存在的问题是您在双打上进行计算,但是您将文本解析为整数。将Integer.parseInt替换为Double.parseDouble,它将按您的意愿执行。

答案 1 :(得分:1)

/ *简单计算器已完成工作* /

   import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Calculator implements ActionListener
    {
    JFrame f;
    JTextField tf;
    JButton num[];
    JButton btNum0;
    JButton btEqual;
    JButton btAdd;
    JButton btSubtract;
    JButton btMultiply;
    JButton btDivide;
    JButton btSolve;
    JButton btClear;
    JButton btDot;
    JButton btExponent;
    double TEMP;
    double SolveTEMP;
    String display="";
    Boolean addBool = false ;
    Boolean subBool = false ;
    Boolean divBool = false ;
    Boolean mulBool = false ;
    Boolean expBool = false ;     
    Calculator(String s)
    {
    String number[]={"1","2","3","4","5","6","7","8","9"};
    int m=0;
    int x[]={30,80,130};
    int y[]={205,155,105};
    f=new JFrame(s);
       try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
          System.out.println("Error setting native LAF: " + e);
        }
    JLabel l=new JLabel("Welcome To ZeeCalc Calculator.. :)");
    l.setBounds(30,10,240,40);
    f.add(l);
    tf=new JTextField();
    num=new JButton[number.length];
    tf.setBounds(30,50,240,40);
    f.add(tf);
    tf.setEditable(false);
    for(int j=0;j<x.length;j++)
    {
    for(int i=0;i<y.length;i++)
    {
    if(m<num.length)
    {
    num[m]=new JButton(String.valueOf(m+1));
    num[m].setBounds(x[i],y[j],45,45);
    f.add(num[m]);
    }
    m++;
    }
    }

    btNum0=new JButton("0");
    btNum0.setBounds(30,255,95,45);
    f.add(btNum0);

    btAdd=new JButton("+");
    btAdd.setBounds(180,255,45,45);
    f.add(btAdd);

    btSubtract=new JButton("-");
    btSubtract.setBounds(180,205,45,45);
    f.add(btSubtract);

    btMultiply=new JButton("*");
    btMultiply.setBounds(180,155,45,45);
    f.add(btMultiply);

    btDot=new JButton(".");
    btDot.setBounds(230,155,45,45);
    f.add(btDot);

    btDivide=new JButton("/");
    btDivide.setBounds(180,105,45,45);
    f.add(btDivide);

    btExponent=new JButton("^");
    btExponent.setBounds(230,105,45,45);
    f.add(btExponent);


    btSolve=new JButton("=");
    btSolve.setBounds(230,205,45,95);
    f.add(btSolve);


    btClear=new JButton("C");
    btClear.setBounds(130,255,45,45);
    f.add(btClear);



    m=0;
    for(int i=0;i<x.length;i++)
    {
    for(int j=0;j<y.length;j++)
    {
    if(m<num.length)
    {
    num[m].addActionListener(this);
    }
    m++;
    }
    }
    btNum0.addActionListener(this);
    btAdd.addActionListener(this);
    btSubtract.addActionListener(this);
    btMultiply.addActionListener(this);
    btDivide.addActionListener(this);
    btSolve.addActionListener(new Operations());
    btClear.addActionListener(this);
    btDot.addActionListener(this);
    btExponent.addActionListener(this);
    f.setLayout(null);
    f.setSize(310,360);
    f.setVisible(true);
    f.setResizable(false);
    f.addWindowListener(new WindowAdapter()
                        {
                        public void windowClosing(WindowEvent e)
                        {
                        System.exit(0);
                        }
                        });
    }




    public void actionPerformed(ActionEvent e)
    {

    if(e.getSource()==num[0])
    {
      display = tf.getText();
      tf.setText(display + "1");
    }
    else if(e.getSource()==num[1])
    {
    display = tf.getText();
      tf.setText(display + "2");
    }
    else if(e.getSource()==num[2])
    {
    display = tf.getText();
      tf.setText(display + "3");
    }
    else if(e.getSource()==num[3])
    {
    display = tf.getText();
      tf.setText(display + "4");
    }
    else if(e.getSource()==num[4])
    {
    display = tf.getText();
      tf.setText(display + "5");
    }
    else if(e.getSource()==num[5])
    {
    display = tf.getText();
      tf.setText(display + "6");
    }
    else if(e.getSource()==num[6])
    {
    display = tf.getText();
      tf.setText(display + "7");
    }
    else if(e.getSource()==num[7])
    {
    display = tf.getText();
      tf.setText(display + "8");
    }
    else if(e.getSource()==num[8])
    {
    display = tf.getText();
      tf.setText(display + "9");
    }
    else if(e.getSource()==btNum0)
    {
    display = tf.getText();
      tf.setText(display + "0");
    }

    else if(e.getSource()==btDot)
    {
    String dot=".";
    display=tf.getText();
    tf.setText(display+ ".");
    }

    else if(e.getSource()==btAdd)
    {
    TEMP=Double.parseDouble(tf.getText());
    tf.setText("");
    addBool=true;
    }
    else if(e.getSource()==btSubtract)
    {
    TEMP=Double.parseDouble(tf.getText());
    tf.setText("");
    subBool=true;
    }
    else if(e.getSource()==btMultiply)
    {
    TEMP=Double.parseDouble(tf.getText());
    tf.setText("");
    mulBool=true;
    }

    else if(e.getSource()==btDivide)
    {
    TEMP=Double.parseDouble(tf.getText());
    tf.setText("");
    divBool=true;
    }
    else if(e.getSource()==btExponent)
    {
    TEMP=Double.parseDouble(tf.getText());
    tf.setText("");
    expBool=true;
    }
    else if(e.getSource()==btClear)
    {
    tf.setText(null);
    }

    }

    public static void main(String... r)
    {
    new Calculator("ZeeCalc Calculator");

    }


    class Operations implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    //double c=1;
    SolveTEMP=Double.parseDouble( tf.getText() );

    if(addBool==true )
    {
    SolveTEMP=SolveTEMP+TEMP;
    }

    else if(subBool==true)
    {
    SolveTEMP=TEMP-SolveTEMP;
    }
    else if(mulBool==true)
    {
    SolveTEMP=SolveTEMP*TEMP;
    }
    else if(divBool==true)
    {
    SolveTEMP=TEMP/SolveTEMP;
    }
    else if(expBool==true)
    {
    double a=TEMP,b=SolveTEMP;
    SolveTEMP=1;
    for(int i=0;i<b;i++)
    {
    SolveTEMP=SolveTEMP*TEMP;
    }
    }
    tf.setText(Double.toString(SolveTEMP));

    addBool = false ;
    subBool = false ;
    mulBool = false ;
    divBool = false ;
    expBool = false ;
    }

    }

    }