GUI简易计算器

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

public class GUIDemo1 extends JFrame {

    double num1;
    double num2;
    int fo;


    public GUIDemo1(){//构造方法  创建窗口



        JFrame jFrame=new JFrame();//创建jFrame窗口
        this.setSize(400,400);//设置窗口大小
        //this.setLocation(100,100);
        this.setLocationRelativeTo(null);//窗口从屏幕中间弹出
        this.setTitle("计算器");//窗口名称
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
        this.setResizable(true);//是否可移动

        JPanel jPanel0=new JPanel(new GridLayout(7,4));//创建一个大面板

        JPanel jPanel1=new JPanel(new FlowLayout(FlowLayout.LEFT));//创建一个面板
        JMenuBar jMenuBar=new JMenuBar();//创建菜单栏
        JMenu jMenu1=new JMenu("查看");
        JMenu jMenu2=new JMenu("编辑");
        JMenu jMenu3=new JMenu("帮助");
        JMenuItem jMenuItem1=new JMenuItem("自己不会查资料吗");
        jMenu1.add(jMenuItem1);
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);
        jMenuBar.add(jMenu3);
        jPanel1.add(jMenuBar);
        jPanel0.add(jPanel1);

        JPanel jPanel2=new JPanel(new FlowLayout());//创建一个面板

        //设置一个文本框
        JLabel point1=new JLabel();
        JTextField pointField1=new JTextField("0",20);
        jPanel2.add(point1);
        jPanel2.add(pointField1);//把文本框添加到面板上



        JPanel jPanel3=new JPanel();//创建一个面板
        jPanel3.setLayout(new GridLayout(1,4,5,5));// 面板表格布局  四行四列 间隔为5

        JButton jButton1=new JButton("PI");
        JButton jButton2=new JButton("C");
        JButton jButton3=new JButton("后退");
        JButton jButton4=new JButton("=");
        jPanel3.add(jButton1);
        jPanel3.add(jButton2);
        jPanel3.add(jButton3);
        jPanel3.add(jButton4);


        JPanel jPanel4=new JPanel();//创建一个面板
        jPanel4.setLayout(new GridLayout(1,4,5,5));// 面板表格布局  四行四列 间隔为5

        JButton jButton5=new JButton("7");
        JButton jButton6=new JButton("8");
        JButton jButton7=new JButton("9");
        JButton jButton8=new JButton("+");
        jPanel4.add(jButton5);
        jPanel4.add(jButton6);
        jPanel4.add(jButton7);
        jPanel4.add(jButton8);

        JPanel jPanel5=new JPanel();//创建一个面板
        jPanel5.setLayout(new GridLayout(1,4,5,5));// 面板表格布局  四行四列 间隔为5

        JButton jButton9=new JButton("4");
        JButton jButton10=new JButton("5");
        JButton jButton11=new JButton("6");
        JButton jButton12=new JButton("-");
        jPanel5.add(jButton9);
        jPanel5.add(jButton10);
        jPanel5.add(jButton11);
        jPanel5.add(jButton12);

        JPanel jPanel6=new JPanel();//创建一个面板
        jPanel6.setLayout(new GridLayout(1,4,5,5));// 面板表格布局  四行四列 间隔为5

        JButton jButton13=new JButton("1");
        JButton jButton14=new JButton("2");
        JButton jButton15=new JButton("3");
        JButton jButton16=new JButton("*");
        jPanel6.add(jButton13);
        jPanel6.add(jButton14);
        jPanel6.add(jButton15);
        jPanel6.add(jButton16);

        JPanel jPanel7=new JPanel();//创建一个面板
        jPanel7.setLayout(new GridLayout(1,4,5,5));// 面板表格布局  四行四列 间隔为5

        JButton jButton17=new JButton("%");
        JButton jButton18=new JButton("0");
        JButton jButton19=new JButton(".");
        JButton jButton20=new JButton("/");
        jPanel7.add(jButton17);
        jPanel7.add(jButton18);
        jPanel7.add(jButton19);
        jPanel7.add(jButton20);

        //添加到大面板
        jPanel0.add(jPanel1);
        jPanel0.add(jPanel2);
        jPanel0.add(jPanel3);
        jPanel0.add(jPanel4);
        jPanel0.add(jPanel5);
        jPanel0.add(jPanel6);
        jPanel0.add(jPanel7);

        this.add(jPanel0);//大面板添加到窗口
        this.setVisible(true);
        //事件监听
        jButton1.addActionListener(new ActionListener() {//PI
            @Override
            public void actionPerformed(ActionEvent e) {

                if(pointField1.getText().equals("0")){
                    pointField1.setText("3.14");
                }
                else pointField1.setText(pointField1.getText()+3.14);

            }
        });

        jButton2.addActionListener(new ActionListener() {//清除
            @Override
            public void actionPerformed(ActionEvent e) {
                pointField1.setText("0");
            }
        });

        jButton3.addActionListener(new ActionListener() {//后退
            @Override
            public void actionPerformed(ActionEvent e) {
if(pointField1.getText().equals("0")){//就不管他

}else pointField1.setText(pointField1.getText().substring(0,pointField1.getText().length()-1));

            }
        });

        jButton4.addActionListener(new ActionListener() {//=
            @Override
            public void actionPerformed(ActionEvent e) {
//jisuanshow();
               num2=Double.parseDouble(pointField1.getText());
      //    num2=Integer.parseInt(pointField1.getText());
                double result;

                switch (fo){
                    case 1:
                        //pointField1.setText(String.valueOf(num1+num2));//为什么就不对
                        result=num1+num2;
                        pointField1.setText(String.valueOf(result));
                        break;
                    case 2:
                        result=num1-num2;
                        pointField1.setText(String.valueOf(result));
                        break;
                    case 3:
                        result=num1*num2;
                        pointField1.setText(String.valueOf(result));
                        break;
                    case 4:
                        result=num1/num2;
                        pointField1.setText(String.valueOf(result));
                        break;
                }

            }
        });

        jButton5.addActionListener(new ActionListener() {//7
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")) {
                    pointField1.setText(jButton5.getText());
                }else
                     pointField1.setText(pointField1.getText() + jButton5.getText());

            }
        });

        jButton6.addActionListener(new ActionListener() {//8
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pointField1.getText().equals("0")) {
                    pointField1.setText((jButton6.getText()));
                }
                else pointField1.setText(pointField1.getText() + jButton6.getText());

            }
        });

        jButton7.addActionListener(new ActionListener() {//9
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton7.getText());
                }

                else pointField1.setText(pointField1.getText()+jButton7.getText());

            }
        });

        jButton8.addActionListener(new ActionListener() {//+
            @Override
            public void actionPerformed(ActionEvent e) {
//jisuanshow();

              num1=Double.parseDouble(pointField1.getText());
           //     num1=Integer.parseInt(pointField1.getText());
//                pointField1.setText("0");

                fo=1;
                pointField1.setText("0");
            }
        });

        jButton9.addActionListener(new ActionListener() {//4
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton9.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton9.getText());


            }
        });

        jButton10.addActionListener(new ActionListener() {//5
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton10.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton10.getText());


            }
        });

        jButton11.addActionListener(new ActionListener() {//6
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton11.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton11.getText());

            }
        });

        jButton12.addActionListener(new ActionListener() {//-
            @Override
            public void actionPerformed(ActionEvent e) {
//jisuanshow();
                num1=Double.parseDouble(pointField1.getText());

                fo=2;
                pointField1.setText("0");
            }
        });

        jButton13.addActionListener(new ActionListener() {//1
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton13.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton13.getText());

            }
        });

        jButton14.addActionListener(new ActionListener() {//2
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton14.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton14.getText());

            }
        });

        jButton15.addActionListener(new ActionListener() {//3
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton15.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton15.getText());

            }
        });

        jButton16.addActionListener(new ActionListener() {//*
            @Override
            public void actionPerformed(ActionEvent e) {
//                jisuanshow();
num1=Double.parseDouble(pointField1.getText());
pointField1.setText("0");
fo=3;
            }
        });

        jButton17.addActionListener(new ActionListener() {//%取余
            @Override
            public void actionPerformed(ActionEvent e) {
               Double num=Double.parseDouble(pointField1.getText());
               double quyu;
                quyu=num/100;
                pointField1.setText(String.valueOf(quyu));

                pointField1.setText(pointField1.getText());


            }
        });

        jButton18.addActionListener(new ActionListener() {//0
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pointField1.getText().equals("0")){
                    pointField1.setText(jButton18.getText());
                }
                else pointField1.setText(pointField1.getText()+jButton18.getText());

            }
        });

        jButton19.addActionListener(new ActionListener() {//.
            @Override
            public void actionPerformed(ActionEvent e) {
                pointField1.setText(pointField1.getText()+jButton19.getText());

            }
        });

        jButton20.addActionListener(new ActionListener() {//  /
            @Override
            public void actionPerformed(ActionEvent e) {
//                jisuanshow();
                num1=Double.parseDouble(pointField1.getText());
                pointField1.setText("0");
                fo=4;

            }


        });



    }

    public static void main(String[] args) {

        GUIDemo1 jFrame=new GUIDemo1();
    }

}

运行界面:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

团团kobebryant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值