冰凌汇编

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 33|回复: 0
收起左侧

[Java] java简易计算器

[复制链接]
IMG1257 发表于 2022-7-17 23:19:57
初学java的GUI编程,只支持加法
java简易计算器 - IMG1257_冰凌汇编
[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) {
 
    }
}
冰凌汇编免责声明
以上内容均来自网友转发或原创,如存在侵权请发送到站方邮件9003554@qq.com处理。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|站点统计|Archiver|小黑屋|RSS|冰凌汇编 ( 滇ICP备2022002049号 滇公网安备 53032102000029号)|网站地图

GMT+8, 2022-9-25 05:51 , Processed in 0.138846 second(s), 9 queries , Redis On.

冰凌汇编 - 建立于2021年12月20日

Powered by Discuz! © 2001-2022 Comsenz Inc.

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