吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1078|回复: 8
收起左侧

[学习记录] java简易计算器

[复制链接]
hackerbob 发表于 2022-7-17 22:03
初学java的GUI编程,只支持加法
Snipaste_2022-07-17_22-00-40.png

[Java] 纯文本查看 复制代码
import java.awt.*;
import java.awt.event.*;

public class calculator {
    public static void main(String[] args) {
        new mycalc();
    }
}
class mycalc extends Frame {
    public mycalc(){
        Button button1 = new Button("compute");
        Button button2 = new Button("empty");
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(10);
        Label label1 = new Label("+");
        Label label2 = new Label("=");
        this.setTitle("计算器");
        this.setBackground(new Color(255, 255, 255));
        this.setBounds(500,500,236,80);
        this.setVisible(true);
        this.setLayout(new FlowLayout());
        this.add(textField1);
        this.add(label1);
        this.add(textField2);
        this.add(label2);
        this.add(textField3);
        this.add(button1);
        this.add(button2);
        this.pack();
        mywframe myframe = new mywframe();
        this.addWindowListener(myframe);
        mybuttonlistener mybuttonlistener = new mybuttonlistener(textField1,textField2,textField3);
        button1.addActionListener(mybuttonlistener);
        mybutton2listener mybutton2listener = new mybutton2listener(textField1,textField2,textField3);
        button2.addActionListener(mybutton2listener);
    }
}
class mybuttonlistener implements ActionListener {
    private TextField num4;
    private TextField num5;
    private TextField num6;
    public mybuttonlistener(TextField num1,TextField num2,TextField num3){
        this.num4 = num1;
        this.num5 = num2;
        this.num6 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int num7 = Integer.parseInt(num4.getText());
        int num8 = Integer.parseInt(num5.getText());
        num6.setText(""+(num7+num8));
    }
}
class mybutton2listener implements ActionListener{
    private TextField num4;
    private TextField num5;
    private TextField num6;
    public mybutton2listener(TextField num1,TextField num2,TextField num3){
        this.num4 = num1;
        this.num5 = num2;
        this.num6 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        num4.setText("");
        num5.setText("");
        num6.setText("");
    }
}
class mywframe implements WindowListener{
    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}

免费评分

参与人数 1吾爱币 +1 收起 理由
skywalker0123 + 1 做的不错

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

skywalker0123 发表于 2022-7-17 22:19
据我所知,Python终端都可以直接做加减乘除
guo0328 发表于 2022-7-17 22:31
AiniWang 发表于 2022-7-18 09:27
本帖最后由 AiniWang 于 2022-7-18 09:30 编辑

[Java] 纯文本查看 复制代码
// GUI简易计算器实现
public class TestCalcDemo {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

class Calculator extends Frame {
    TextField num1, num2, num3;

    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10); 
        num3 = new TextField(20); 
        Button button = new Button("=");
        button.setActionCommand("=");
        button.addActionListener(new MyCalculatorListener());
        Label label = new Label("+");
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        
        setTitle("简易计算器-加法");
        setVisible(true);
        setBounds(300, 200, 500, 80);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("窗口关闭监听事件触发");
                System.exit(0);
            }
        });
    }

    // 监听器类
    private class MyCalculatorListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!num1.getText().equals("")) {
                int n1 = Integer.parseInt(num1.getText());
                if (!num2.getText().equals("")) {
                    int n2 = Integer.parseInt(num2.getText());
                    num3.setText(String.valueOf(n1 + n2));
                    num1.setText("");
                    num2.setText("");
                } else {
                    System.out.println("被加数获取到为空");
                }
            } else {
                System.out.println("加数获取到为空");
            }
        }
    }
}

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
hackerbob + 2 + 1 我很赞同!

查看全部评分

erui 发表于 2022-7-18 10:30
有意思的计算器,是java编出来的计算器。
Vvvvvoid 发表于 2022-7-18 10:32
skywalker0123 发表于 2022-7-17 22:19
据我所知,Python终端都可以直接做加减乘除

java 终端也可以
Todd 发表于 2022-7-18 12:07
skywalker0123 发表于 2022-7-17 22:19
据我所知,Python终端都可以直接做加减乘除

java 11 有个jshell 有点跟python终端差不多了
skywalker0123 发表于 2022-7-18 12:08
Todd 发表于 2022-7-18 12:07
java 11 有个jshell 有点跟python终端差不多了

确实啊!
5号军 发表于 2022-8-1 19:36
有意思的计算器,是java编出来的计算器。7

java 11 有个jshell 有点跟python终端差不多了

确实啊!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 09:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表