java swing网格布局 计算器_用JAVA网格布局写一个简单的计算器

展开全部

无聊写了个,修复了下32313133353236313431303231363533e78988e69d8331333337623530Bug:

b41f862ac355e422886a1dff21abfc1a.png

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField; public class Calculate extends JFrame implements ActionListener {     private static final long serialVersionUID = 1L;     private JButton plus, reduce, multiply, divice, reset;    private JTextField one, two, result;    private boolean device_falg = false;     private final int width = 400, height = 300;     public Calculate() {        super("修改密码");        this.setLayout(null);        this.setSize(width, height);        init();        Layout();    }     public void init() {        plus = new JButton("加   ");        reduce = new JButton("减    ");        multiply = new JButton("乘   ");        divice = new JButton("除    ");        reset = new JButton("清空");        one = new JTextField();        two = new JTextField();        result = new JTextField();    }     public void Layout() {        this.add(new JLabel("第一个数")).setBounds(20, 10, 60, 80);        this.add(one).setBounds(100, 38, 100, 25);        this.add(new JLabel("第二个数")).setBounds(20, 40, 60, 80);        this.add(two).setBounds(100, 70, 100, 25);        this.add(new JLabel("结果")).setBounds(20, 85, 60, 80);        this.add(result).setBounds(100, 110, 100, 25);         this.add(plus).setBounds(70, 170, 80, 25);        this.add(reduce).setBounds(200, 170, 80, 25);        this.add(multiply).setBounds(70, 200, 80, 25);        this.add(divice).setBounds(200, 200, 80, 25);         this.add(reset).setBounds(300, 220, 80, 25);         plus.addActionListener(this);        reduce.addActionListener(this);        multiply.addActionListener(this);        divice.addActionListener(this);        reset.addActionListener(this);         this.setVisible(true);        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);    }     public boolean Format() {        boolean FLAG = false;        boolean flag = false;        String one = this.one.getText().toString().trim();        String two = this.two.getText().toString().trim();         if (one == null || one.equals("") || two == null || two.equals("")) {            JOptionPane.showMessageDialog(getParent(), "请输入完整信息!");            FLAG = false;            flag = true;        }         boolean boll_1 = one.matches("[\\d]{1,100}");        boolean boll_2 = two.matches("[\\d]{1,100}");        boolean boll_3 = one.matches("[\\d]{1,100}+[.]+[\\d]{1,100}");        boolean boll_4 = two.matches("[\\d]{1,100}+[.]+[\\d]{1,100}");        if (flag) {            return false;        }        if ((boll_1 && boll_2) || (boll_3 && boll_4) || (boll_1 && boll_4)                || (boll_3 && boll_2)) {            FLAG = true;        } else {            JOptionPane.showMessageDialog(getParent(), "请输入数字!");            FLAG = false;        }         if (FLAG && device_falg) {            if (Double.parseDouble(two) == 0) {                JOptionPane.showMessageDialog(getParent(), "被除数不能为0!");                FLAG = false;                device_falg=false;            }        }         return FLAG;    }     public double Plus(double one, double two) {        return one + two;    }     public double Multiply(double one, double two) {        return one * two;    }     public double Divice(double one, double two) {        return one / two;    }     public double Reduce(double one, double two) {        return one - two;    }     public void Clear() {        one.setText("");        two.setText("");        result.setText("");    }     @Override    public void actionPerformed(ActionEvent e) {        Object o = e.getSource();        if (o == reset) {            Clear();            return;        }        if (o == divice) {            device_falg = true;        }        if (!Format()) {            return;        }        double one = Double.parseDouble(this.one.getText());        double two = Double.parseDouble(this.two.getText());        double result = 0;        if (o == plus) {            result = Plus(one, two);        } else if (o == reduce) {            result = Reduce(one, two);        } else if (o == multiply) {            result = Multiply(one, two);        } else if (o == divice) {            result = Divice(one, two);        }        this.result.setText("" + result);    }    public static void main(String[] args) {        new Calculate();    }}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值