简易计算器的实现(Java)使用awt与swing

这个博客展示了如何使用Java Swing库创建一个简易的四则运算计算器。程序包括输入框、按钮以及处理加减乘除操作的事件监听器,能够进行基本的算术计算并显示结果。同时,它具备错误处理机制,当用户输入不合法数据时会给出提示。
摘要由CSDN通过智能技术生成

 

//package swing_test;

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

class swing {
    public void init() {
        JFrame f = new JFrame("test");
        f.setTitle("简易计算器");
        f.setSize(500, 300);
        f.setLocationRelativeTo(null);

        Font font = new Font("楷体", Font.PLAIN, 40);
        Font font1 = new Font("楷体", Font.PLAIN, 25);

        JPanel panel = new JPanel(new BorderLayout());
        JLabel t = new JLabel("四则运算", JLabel.CENTER);
        t.setFont(font);
        panel.add(t, BorderLayout.NORTH);

        JLabel l1 = new JLabel("第一个数:");
        l1.setFont(font1);
        JLabel l2 = new JLabel("第二个数:");
        l2.setFont(font1);
        JLabel l = new JLabel(" 结 果:");
        l.setFont(font1);
        JLabel l3 = new JLabel("*");
        l3.setFont(font1);
        JLabel l4 = new JLabel("*");
        l4.setFont(font1);
        JButton b1 = new JButton("+");
        b1.setFont(font1);
        JButton b2 = new JButton("-");
        b2.setFont(font1);
        JButton b3 = new JButton("*");
        b3.setFont(font1);
        JButton b4 = new JButton("/");
        b4.setFont(font1);
        JButton b5=new JButton("清除");
        b5.setFont(font1);
        b5.setBackground(Color.GRAY);
        l3.setForeground(Color.red);
        l4.setForeground(Color.red);
        JTextField in1 = new JTextField(20);
        in1.setFont(font1);
        JTextField in2 = new JTextField(20);
        in2.setFont(font1);
        JTextField in3 = new JTextField(20);
        in3.setFont(font1);

        JPanel p = new JPanel();
        p.setLayout(null);
        l1.setBounds(65, 10, 180, 30);
        in1.setBounds(215, 10, 150, 30);
        l3.setBounds(385, 10, 30, 30);
        l2.setBounds(65, 50, 180, 30);
        in2.setBounds(215, 50, 150, 30);
        l4.setBounds(385, 50, 30, 30);
        l.setBounds(65, 90, 180, 30);
        in3.setBounds(215, 90, 150, 30);
        b1.setBounds(40, 145, 50, 50);
        b2.setBounds(115, 145, 50, 50);
        b3.setBounds(190, 145, 50, 50);
        b4.setBounds(265, 145, 50, 50);
        b5.setBounds(350,145,100,50);



/*        JLabel label = new JLabel(" ");
        label.setFont(font1);
        label.setForeground(Color.red);*/

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    in3.setText(String.valueOf(Float.parseFloat(in1.getText()) + Float.parseFloat(in2.getText())));
                } catch (NumberFormatException q) {
                    JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    in3.setText(String.valueOf(Float.parseFloat(in1.getText()) - Float.parseFloat(in2.getText())));
                } catch (NumberFormatException q) {
//                    label.setText("请输入数据后再进行运算!");
                    JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    in3.setText(String.valueOf(Float.parseFloat(in1.getText()) * Float.parseFloat(in2.getText())));
                }catch (NumberFormatException q){
//                    label.setText("请输入数据后再进行运算!");
                    JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    in3.setText(String.valueOf(Float.parseFloat(in1.getText()) / Float.parseFloat(in2.getText())));
                }catch (NumberFormatException q){
//                    label.setText("请输入数据后再进行运算!");
                    JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        b5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                in1.setText("");
                in2.setText("");
                in3.setText("");
                JOptionPane.showMessageDialog(null, "清除已完成!", "清除", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        panel.add(p, BorderLayout.CENTER);
//        panel.add(label, BorderLayout.SOUTH);

        p.add(l1);
        p.add(in1);
        p.add(l3);
        p.add(l2);
        p.add(in2);
        p.add(l4);
        p.add(l);
        p.add(in3);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        p.add(b5);

        f.add(panel);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

public class Demo {
    public static void main(String[] args) {
        swing s = new swing();
        s.init();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值