java实现简单计算器(GUI)

不多说了直接上源码,都在注释里:

package S9;

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

public class S9_4 {
    static Double a;
    static Double b;
    static String string="";
    static String fuhao="";
    public static void main(String[] args) {
        JFrame frame=new JFrame();//实例化一个窗口
        GridLayout layout=new GridLayout(2,1);//实例化一个网格布局管理器
        frame.setLayout(layout);//窗口应用布局管理器
        frame.setSize(300,200);//设置窗口宽高
        frame.setLocation(500,300);//设置窗口初始显示位置
        frame.setVisible(true);//将窗口设置为可见
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭的操作

        //上边面板
        JPanel panel1=new JPanel();
        //记录文本框
        JTextField textField1=new JTextField(20);
        textField1.setHorizontalAlignment(SwingConstants.RIGHT);//将文本水平对齐方式设置为靠右
        textField1.setEditable(false);//设置为不可编辑
        JLabel label=new JLabel("记录");
        //计数文本框
        JTextField textField=new JTextField(20);
        textField.setHorizontalAlignment(SwingConstants.RIGHT);//将文本水平对齐方式设置为靠右
        textField.setEditable(false);//设置为不可编辑
        //添加一个Clear按钮
        JButton buttonClear=new JButton("Clear");
        //将上述控件添加到panel1面板
        panel1.add(textField1);
        panel1.add(label);
        panel1.add(textField);
        panel1.add(buttonClear);
        //为Clear按钮注册监听器
        buttonClear.addActionListener(e->{
            textField.setText("");
            textField1.setText("");
            a=0.0;
            b=0.0;
            string="";
            fuhao="";
        });
        frame.add(panel1);//将panel1面板添加到窗口上

        //下边面板
        JPanel panel2=new JPanel();
        GridLayout gridLayout=new GridLayout(4,4);//网格布局管理器
        panel2.setLayout(gridLayout);//应用该布局管理器
        //实例化一些按钮
        JButton button7=new JButton("7");
        panel2.add(button7);
        JButton button8=new JButton("8");
        panel2.add(button8);
        JButton button9=new JButton("9");
        panel2.add(button9);
        JButton buttonChu=new JButton("/");
        panel2.add(buttonChu);
        JButton button4=new JButton("4");
        panel2.add(button4);
        JButton button5=new JButton("5");
        panel2.add(button5);
        JButton button6=new JButton("6");
        panel2.add(button6);
        JButton buttonCheng=new JButton("*");
        panel2.add(buttonCheng);
        JButton button1=new JButton("1");
        panel2.add(button1);
        JButton button2=new JButton("2");
        panel2.add(button2);
        JButton button3=new JButton("3");
        panel2.add(button3);
        JButton buttonJian=new JButton("-");
        panel2.add(buttonJian);
        JButton button0=new JButton("0");
        panel2.add(button0);
        JButton buttonDian=new JButton(".");
        panel2.add(buttonDian);
        JButton buttonDeng=new JButton("=");
        panel2.add(buttonDeng);
        JButton buttonJia=new JButton("+");
        panel2.add(buttonJia);

        //为每一个数字按钮注册监听器
        button0.addActionListener(e->{
            string=string+"0";
            textField.setText(string);
        });

        button1.addActionListener(e->{
            string=string+"1";
            textField.setText(string);
        });

        button2.addActionListener(e->{
            string=string+"2";
            textField.setText(string);
        });

        button3.addActionListener(e->{
            string=string+"3";
            textField.setText(string);
        });

        button4.addActionListener(e->{
            string=string+"4";
            textField.setText(string);
        });

        button5.addActionListener(e->{
            string=string+"5";
            textField.setText(string);
        });

        button6.addActionListener(e->{
            string=string+"6";
            textField.setText(string);
        });

        button7.addActionListener(e->{
            string=string+"7";
            textField.setText(string);
        });

        button8.addActionListener(e->{
            string=string+"8";
            textField.setText(string);
        });

        button9.addActionListener(e->{
            string=string+"9";
            textField.setText(string);
        });

        buttonDian.addActionListener(e->{
            string=string+".";
            textField.setText(string);
        });

        //为符号键注册监听器
        buttonJia.addActionListener(e->{
            a=Double.parseDouble(textField.getText());//获取第一个操作数
            fuhao="+";//获取符号
            textField1.setText(textField.getText()+fuhao);//将其显示在记录文本框
            textField.setText("");//然后清空关于第一个操作数的记录,方便后续的连续计算
            string="";
        });

        buttonJian.addActionListener(e->{
            a=Double.parseDouble(textField.getText());
            fuhao="-";
            textField1.setText(textField.getText()+fuhao);
            textField.setText("");
            string="";
        });

        buttonCheng.addActionListener(e->{
            a=Double.parseDouble(textField.getText());
            fuhao="*";
            textField1.setText(textField.getText()+fuhao);
            textField.setText("");
            string="";
        });

        buttonChu.addActionListener(e->{
            a=Double.parseDouble(textField.getText());
            fuhao="/";
            textField1.setText(textField.getText()+fuhao);
            textField.setText("");
            string="";
        });
        
        buttonDeng.addActionListener(e->{
            string="";
            b=Double.parseDouble(textField.getText());//记录第二个操作数
            textField1.setText(textField1.getText()+textField.getText()+"=");//显示完整运算式
            //下面是对应符号的运算操作
            switch (fuhao){
                case "+":{
                    Double result=a+b;
                    textField.setText(Double.toString(result));
                    break;
                }
                case "-":{
                    Double result=a-b;
                    textField.setText(Double.toString(result));
                    break;
                }
                case "*":{
                    Double result=a*b;
                    textField.setText(Double.toString(result));
                    break;
                }
                case "/":{
                    Double result=a/b;
                    textField.setText(Double.toString(result));
                    break;
                }
            }
        });
        frame.add(panel2);//面板添加到窗口
    }
}

你可能感兴趣的:(java,java,开发语言,后端)