简单GUI制作lj计算器

简单GUI制作lj计算器

废话不多说,直接上代码,有看不懂的部分欢迎私信嗷
(java大作业时写的,本人很菜,如有问题还请大佬指正)

package rg664wxfwgs;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class mycalander{
    public static void main(String[] args) {
        new calander();
    }
}


class calander extends JFrame{
    private StringBuilder sBuilder = new StringBuilder();
    private Double a;//第一个数
    private Double b;//第二个数
    private Double double1;//结果
    private Integer i;// i=0、1、2、3分别代表加减乘除,可以区分未赋值和赋值为0的区别


//构造方法   ************************************
    calander() {
        this.setTitle("lj的lj计算器");
        this.setBounds(1000,300,625, 890);//位置大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //建立容器
        JPanel panel = new JPanel();
        this.getContentPane().add(panel);
        panel.setLayout(null);

// 定义label
        final JLabel label = new JLabel();
        label.setBounds(0, 0, 600, 100);
        label.setFont(new Font("dialog", 1, 70));
        label.setOpaque(true);// 不透明
        label.setBackground(Color.white);//背景色
        panel.add(label);//加入到容器中


// 定义按钮组件************************************
        JButton button1 = new JButton("1");
        JButton button2 = new JButton("2");
        JButton button3 = new JButton("3");
        JButton button4 = new JButton("4");
        JButton button5 = new JButton("5");
        JButton button6 = new JButton("6");
        JButton button7 = new JButton("7");
        JButton button8 = new JButton("8");
        JButton button9 = new JButton("9");
        JButton button0 = new JButton("0");

        JButton buttonadd = new JButton("+");// +
        JButton buttonminus = new JButton("-");// -
        JButton buttontime = new JButton("×");// ×
        JButton buttondivid = new JButton("÷");// ÷
        JButton buttonequal = new JButton("=");// =
        JButton buttondecimal = new JButton(".");// .
        JButton buttondelet = new JButton("←");// 删除一位
        JButton buttonclear = new JButton("C");// 清屏


// 定义按钮大小格式位置,添加到容器************************************
        button0.setBounds(0, 100, 200, 120);
        button0.setFont(new Font("dialog", 1, 50));
        panel.add(button0);
        button1.setBounds(200, 100, 200, 120);
        button1.setFont(new Font("dialog", 1, 50));
        panel.add(button1);
        button2.setBounds(400, 100, 200, 120);
        button2.setFont(new Font("dialog", 1, 50));
        panel.add(button2);
        button3.setBounds(0, 220, 200, 120);
        button3.setFont(new Font("dialog", 1, 50));
        panel.add(button3);
        button4.setBounds(200, 220, 200, 120);
        button4.setFont(new Font("dialog", 1, 50));
        panel.add(button4);
        button5.setBounds(400, 220, 200, 120);
        button5.setFont(new Font("dialog", 1, 50));
        panel.add(button5);
        button6.setBounds(0, 340, 200, 120);
        button6.setFont(new Font("dialog", 1, 50));
        panel.add(button6);
        button7.setBounds(200, 340, 200, 120);
        button7.setFont(new Font("dialog", 1, 50));
        panel.add(button7);
        button8.setBounds(400, 340, 200, 120);
        button8.setFont(new Font("dialog", 1, 50));
        panel.add(button8);
        button9.setBounds(0, 460, 200, 120);
        button9.setFont(new Font("dialog", 1, 50));
        panel.add(button9);
        buttonadd.setBounds(200, 460, 200, 120);
        buttonadd.setFont(new Font("dialog", 1, 50));


        panel.add(buttonadd);//加
        buttonminus.setBounds(400, 460, 200, 120);
        buttonminus.setFont(new Font("dialog", 1, 50));
        panel.add(buttonminus);//减
        buttontime.setBounds(0, 580, 200, 120);
        buttontime.setFont(new Font("dialog", 1, 50));
        panel.add(buttontime);//乘
        buttondivid.setBounds(200, 580, 200, 120);
        buttondivid.setFont(new Font("dialog", 1, 50));
        panel.add(buttondivid);//除
        buttonequal.setBounds(400, 580, 200, 120);
        buttonequal.setFont(new Font("dialog", 1, 50));
        panel.add(buttonequal);//等于
        buttondecimal.setBounds(0, 700, 200, 120);
        buttondecimal.setFont(new Font("dialog", 1, 50));
        panel.add(buttondecimal);//小数点
        buttonclear.setBounds(200, 700, 200, 120);
        buttonclear.setFont(new Font("dialog", 1, 50));
        panel.add(buttonclear);//复位
        buttondelet.setBounds(400, 700, 200, 120);
        buttondelet.setFont(new Font("dialog", 1, 50));
        panel.add(buttondelet);//删除


// 给各个按钮设置动作监听器 , 输入数值操作0~9  ************************************
        button0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("0"); //追加append
                label.setText(sBuilder.toString()); //在label中显示输出
            }
        });
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("1");
                label.setText(sBuilder.toString());
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("2");
                label.setText(sBuilder.toString());
            }
        });
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("3");
                label.setText(sBuilder.toString());
            }
        });
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("4");
                label.setText(sBuilder.toString());
            }
        });
        button5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("5");
                label.setText(sBuilder.toString());
            }
        });
        button6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("6");
                label.setText(sBuilder.toString());
            }
        });
        button7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("7");
                label.setText(sBuilder.toString());
            }
        });
        button8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("8");
                label.setText(sBuilder.toString());
            }
        });
        button9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append("9");
                label.setText(sBuilder.toString());
            }
        });


// 点击运算符 0--3      ************************************
        buttonadd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                a = Double.parseDouble(sBuilder.toString());//第一个数的赋值
                sBuilder = new StringBuilder();
                label.setText("+");//打印出算符
                i = 0;
            }
        });

        buttonminus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                a = Double.parseDouble(sBuilder.toString());
                sBuilder = new StringBuilder();
                label.setText("-");
                i = 1;
            }
        });

        buttontime.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                a = Double.parseDouble(sBuilder.toString());
                sBuilder = new StringBuilder();
                label.setText("×");
                i = 2;
            }
        });

        buttondivid.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                a = Double.parseDouble(sBuilder.toString());
                sBuilder = new StringBuilder();
                label.setText("÷");
                i = 3;
            }
        });

        buttonequal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

                if (""!=sBuilder.toString() && (a != 0.0))//判断a是否为0,sbuilder是否未输入
                {
                    b = Double.parseDouble(sBuilder.toString());//第二个数的赋值
                    if (i == 0)              //+
                    {
                        double1 = a + b;//计算
                        label.setText(a +"+"+ b+"="+double1.toString());//打印
                        sBuilder = new StringBuilder();//初始化
                        sBuilder.append(double1);//修改实际的值,但不打印
                    }
                    else if (i == 1)         //-
                    {
                        double1 = a - b;
                        label.setText(a +"-"+ b+"="+double1.toString());
                        sBuilder = new StringBuilder();
                        sBuilder.append(double1);
                    }
                    else if (i == 2)            //×
                    {
                        double1 = a * b;
                        label.setText(a +"×"+ b+"="+double1.toString());
                        sBuilder = new StringBuilder();
                        sBuilder.append(double1);
                    }
                    else if (i == 3)              //÷
                    {
                        double1 = a / b;
                        label.setText(a +"÷"+ b+"="+double1.toString());
                        sBuilder = new StringBuilder();
                        sBuilder.append(double1);
                    }
                    else
                    {
                        label.setText(sBuilder.toString());
                    }
                }
            }
        });

        buttondecimal.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder.append(".");
                label.setText(sBuilder.toString());
            }
        });

        buttonclear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sBuilder = new StringBuilder();
                label.setText("");
            }
        });

        buttondelet.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!"".equals(sBuilder.toString())) {
                    sBuilder.deleteCharAt(sBuilder.length() - 1);//通过减长来删除最后一位
                    label.setText(sBuilder.toString());
                }
            }
        });

        this.setVisible(true);//设置为可见
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值