JAVA实现简易计算器

JAVA实现简易计算器

利用AWT和swing实现可视化简易计算器

/*
Author:cxc
Date:2021.04.07
Func:简易计算器
*/
package com.company.test0407;

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

public class Main {

    public static void main(String[] arg) {
   // write your code here
        myFarm farm = new myFarm();
    }
}

class myFarm extends JFrame{
    //网格化布局
    JPanel  jP1  = new JPanel();
    JPanel  jP2  = new JPanel();
    JPanel  jP3  = new JPanel();
    JTextField jTF1 = new JTextField();
    JTextField jTF2 = new JTextField();
    JButton button1 = new MyButton("+");
    JButton button2 = new MyButton("-");
    JButton button3 = new MyButton("*");
    JButton button4 = new MyButton("/");
    JButton button5 = new MyButton("%");
    JLabel jL = new JLabel("结果为:");
    JTextArea jTF3 = new JTextArea();
    ButtonListener1 listener1= new ButtonListener1();
    ButtonListener2 listener2= new ButtonListener2();
    ButtonListener3 listener3= new ButtonListener3();
    ButtonListener4 listener4= new ButtonListener4();
    ButtonListener5 listener5= new ButtonListener5();
    private String s1;
    private String s2;
    private String s3;
    private float a;
    private float b;
    private float c;

    myFarm() {
        this.setTitle("计算器V1.0 Author:cxc");
        this.setSize(400,200);
        this.setLocationRelativeTo(getOwner());
        this.setLayout(new GridLayout(3,5,5,5));
        jP1.setLayout(new GridLayout(1,2,5,5));
        jP2.setLayout(new GridLayout(1,5,5,5));
        jP3.setLayout(new GridLayout(1,2,5,5));
        this.add(jP1);
        this.add(jP2);
        this.add(jP3);

        jP1.add(jTF1);
        jP1.add(jTF2);

        jP2.add(button1);
        jP2.add(button2);
        jP2.add(button3);
        jP2.add(button4);
        jP2.add(button5);
        jP3.add(jL);
        jTF3.setForeground(Color.BLUE);
        jP3.add(jTF3);
        button1.addActionListener(listener1);
        button2.addActionListener(listener2);
        button3.addActionListener(listener3);
        button4.addActionListener(listener4);
        button5.addActionListener(listener5);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    //新建操作符监听器,重写监听器执行动作actionPerformed
    class ButtonListener1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            try {
                s1 = jTF1.getText();
                s2 = jTF2.getText();
                a = Float.parseFloat(s1);
                b = Float.parseFloat(s2);
                c = a + b;
                jTF3.setText(String.format("%.3f", c));
            } catch (Exception e1) {

            }
        }
    }

    class ButtonListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            try {
                s1 = jTF1.getText();
                s2 = jTF2.getText();
                a = Float.parseFloat(s1);
                b = Float.parseFloat(s2);
                c = a - b;
                jTF3.setText(String.format("%.3f", c));
            } catch (Exception e1) {

            }
        }
    }

    class ButtonListener3 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e)
        {

            try{
                s1 = jTF1.getText();
                s2 = jTF2.getText();
                a = Float.parseFloat(s1);
                b = Float.parseFloat(s2);
                c = a * b;
                jTF3.setText(String.format("%.3f",c));
            }
            catch(Exception e1)
            {

            }
        }
    }

    class ButtonListener4 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                s1 = jTF1.getText();
                s2 = jTF2.getText();
                a = Float.parseFloat(s1);
                b = Float.parseFloat(s2);
                c = a / b;
                jTF3.setText(String.format("%.3f", c));
            } catch (Exception e1) {

            }
        }
    }

    class ButtonListener5 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try {
                s1 = jTF1.getText();
                s2 = jTF2.getText();
                a = Float.parseFloat(s1);
                b = Float.parseFloat(s2);
                c = a % b;
                jTF3.setText(String.format("%.3f", c));
            }
            catch(Exception e1)
            {

            }
        }
    }
}
//自定义按钮
class MyButton extends JButton{
    Font f = new Font("楷体",Font.BOLD,20);
    MyButton(String s)
    {
        this.setForeground(Color.RED);
        this.setFont(f);
        this.setText(s);
    }
}

效果如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值