Java | 简易GUI计算器

一、描述

        基于GUI的简易计算器,能够对两个正整数进行相加或相减,并把结果输出到界面。

二、源代码

Calculator.java

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

public class Calculator extends JFrame {
    private final Container container = getContentPane();
    public Calculator(){
        container.setLayout(null);
        setContent();
        setWindow();
    }
    private void setWindow(){
        this.setVisible(true);                                          //窗口可见
        this.setSize(400,450);                             //窗口大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            //窗口可关闭
        this.setLocationRelativeTo(null);                               //窗口居中
        this.setTitle("简易计算器");                                      //窗口标题
    }
    private void setContent(){
        /* 标签 */
        JLabel jLabel1 = new JLabel("计算器");
        jLabel1.setBounds(140,30,100,50);
        jLabel1.setFont(new Font("楷体",Font.PLAIN,20));

        JLabel jLabel2 = new JLabel("运算数一");
        jLabel2.setBounds(60,70,100,50);
        jLabel2.setFont(new Font("楷体",Font.PLAIN,20));

        JLabel jLabel3 = new JLabel("运算数二");
        jLabel3.setBounds(60,110,100,50);
        jLabel3.setFont(new Font("楷体",Font.PLAIN,20));

        JLabel jLabel4 = new JLabel("运算结果");
        jLabel4.setBounds(60,150,100,50);
        jLabel4.setFont(new Font("楷体",Font.PLAIN,20));

        /* 文本域 */
        JTextField jTextField1 = new JTextField(20);
        jTextField1.setBounds(170,80,150,30);

        JTextField jTextField2 = new JTextField(20);
        jTextField2.setBounds(170,120,150,30);

        JTextField jTextField3 = new JTextField(20);
        jTextField3.setBounds(170,160,150,30);

        /* 按钮 */
        JButton jButton1 = new JButton("相加");
        jButton1.setBounds(80,220,80,40);
        jButton1.setFont(new Font("楷体",Font.PLAIN,20));

        JButton jButton2 = new JButton("相减");
        jButton2.setBounds(220,220,80,40);
        jButton2.setFont(new Font("楷体",Font.PLAIN,20));

        JButton jButton3 = new JButton("清零");
        jButton3.setBounds(120,300,140,40);
        jButton3.setFont(new Font("楷体",Font.PLAIN,20));

        /* 相加 按钮的监听事件 */
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text1 = jTextField1.getText();
                    if (text1.equals("")){
                        new ErrorPrompt();
                    }
                    int num1 = Integer.parseInt(text1);

                    String text2 = jTextField2.getText();
                    if (text2.equals("")){
                        new ErrorPrompt();
                    }
                    int num2 = Integer.parseInt(text2);

                    jTextField3.setText("" + (num1+num2));
                }catch (NumberFormatException a){
                    new ErrorPrompt();
                }
            }
        });

        /* 相减 按钮的监听事件 */
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text1 = jTextField1.getText();
                    if (text1.equals("")){
                        new ErrorPrompt();
                    }
                    int num1 = Integer.parseInt(text1);

                    String text2 = jTextField2.getText();
                    if (text2.equals("")){
                        new ErrorPrompt();
                    }
                    int num2 = Integer.parseInt(text2);
                    jTextField3.setText("" + (num1-num2));
                }catch (NumberFormatException a){
                    new ErrorPrompt();
                }
            }
        });

        /* 全部清零 按钮的监听事件 */
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField1.setText("");    //文本域全部清零
                jTextField2.setText("");
                jTextField3.setText("");
            }
        });

        container.add(jLabel1);
        container.add(jLabel2);
        container.add(jLabel3);
        container.add(jLabel4);
        container.add(jTextField1);
        container.add(jTextField2);
        container.add(jTextField3);
        container.add(jButton1);
        container.add(jButton2);
        container.add(jButton3);
    }
}

class ErrorPrompt extends JFrame {
    Container container = getContentPane();
    public ErrorPrompt(){
        container.setLayout(null);
        setWindow();
        setContent();
    }
    private void setWindow(){
        this.setVisible(true);
        this.setSize(300,200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //关闭当前窗口
        this.setTitle("错误提示");
    }
    private void setContent(){
        JLabel jLabel = new JLabel("请输入正整数!");
        jLabel.setBounds(70,30,140,80);
        jLabel.setFont(new Font("楷体",Font.PLAIN,20));
        jLabel.setForeground(Color.red);    //设置字体为红色
        container.add(jLabel);
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        new Calculator();
    }
}

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值