当前位置:网站首页>Simple addition calculator
Simple addition calculator
2022-07-25 10:15:00 【Look at the bugs】
Simple addition calculator
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class demo02 {
public static void main(String[] args) {
new Calculator();
}
}
// Calculator class
class Calculator extends Frame {
public Calculator() {
TextField textField1=new TextField(10);// Number of characters
TextField textField2=new TextField(10);// Number of characters
TextField textField3=new TextField(20);// Number of characters
Button button = new Button("=");
button.addActionListener(new MyCalculator(textField1,textField2,textField3));
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
// Monitor class
class MyCalculator implements ActionListener{
private TextField num1,num2,num3;
// I don't know why I use the constructor to get these three numbers
public MyCalculator(TextField num1,TextField num2,TextField num3) {
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1, Get the addend and the addend
// take String Type into int type
int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
//2, Take this value + After operation , Put it in the third box
num3.setText(""+(n1+n2));
//3, Clear the first two numbers
num1.setText("");
num2.setText("");
}
}
Simple addition calculator ( Access directly with inner classes )
// If you don't finish writing, you will report a null pointer exception , It won't be solved for the time being
package Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class demo02 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
// Calculator class
class Calculator extends Frame {
// attribute
TextField num1,num2,num3;
// Method
public void loadFrame(){
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(20);
Button button=new Button("=");
// button.addActionListener(new MyCalculator(num1,num2,num3));
button.addActionListener(new MyActionListener());
Label label=new Label();
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
// Monitor class
private class MyCalculator implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1, Get the addend and the addend
// take String Type into int type
//2, Take this value + After operation , Put it in the third box
//3, Clear the first two numbers
int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
Type conversion String Convert to Int
Transformation format :
Integer.parseInt() You can put String Type conversion to Int
Usage method : int n1=Integer.parseInt(num1.getText());
边栏推荐
猜你喜欢
![[machine translation] scones -- machine translation with multi tag tasks](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[machine translation] scones -- machine translation with multi tag tasks

conda 配置深度学习环境 pytorch transformers

切换 shell 命令行终端(bash/zsh)后,conda 无法使用: command not found

静态路由的配置(以华为eNSP为例)

UE4 外部打开exe文件

复现 ASVspoof 2021 baseline RawNet2

canal实现mysql数据同步

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

线程池的死锁事件

IDEA整体字体大小修改
随机推荐
Redis使用场景
鼠标监听,画笔
Nodejs初体验
pnpm简述
Is binary cross entropy really suitable for multi label classification?
小程序分享功能
GUI窗口
NPM详解
Dataset 和 Dataloader数据加载
史上最全面的UE4 文件操作,打开,读、写,增、删、改、查
C3D模型pytorch源码逐句详析(三)
JSONObject解析json格式的终极总结
Probabilistic robot learning notes Chapter 2
JDBC总结
关闭brew执行命令时的自动更新
VLAN的配置及其应用(以华为eNSP为例)
Swing组件
链表相关(设计链表及环链表问题)
cookie and session
Redux使用和剖析