summary: Teach you how to use Java AWT to create a simple calculator.
This article is shared from HUAWEI CLOUD Community " Teach you how to use Java AWT to create a simple calculator ", author: Hai Yong.
About AWT
AWT (Abstract Window Toolkit) is an API (Graphical User Interface) based java application that helps to build GUI. GUI uses some graphics to help user interaction. It mainly consists of a set of classes and methods required to create and manage GUI buttons, windows, frames, text boxes, radio buttons and more in a simplified way
The Java code I have provided is for the action listener interface for the calculator for event handling.
logical part
1. For number buttons
if(e.getSource()==b1){ //b1 代表数字 1
zt=l1.getText();
z=zt+"1";// 1 将合并在前一个值的末尾
l1.setText(z);
}
When any number button is pressed, any value in label l1 will be stored in variable zt, then concatenated with the corresponding number, and then displayed in label l1, we do something similar for NEGATIVE and DECIMAL PTS buttons
2. For arithmetic buttons
if(e.getSource()==badd){ //对应加法
num1=Double.parseDouble(l1.getText());
z="";
l1.setText(z);
check=1;
}
Now, after we convert the value of the label l1 to type double, store it into the variable num1, which will technically be the first number, and then set the label l1 to null
We will just use a check variable to get this particular pneumatic button (here +) clicked so we can do this in our = button
3. For the equal button
if(e.getSource()==bcalc){
num2=Double.parseDouble(l1.getText());
if(check==1)
xd =num1+num2;
if(check==2)
xd =num1-num2;
if(check==3)
xd =num1*num2;
if(check==4)
xd =num1/num2;
if(check==5)
xd =num1%num2;
l1.setText(String.valueOf(xd));
}
Now again store the value l1 into the num2 variable, which will be the second number arithmetically, then check the value of the variable, check then do the appropriate action, then display the result in the label l1
4. For clear button
if(e.getSource()==bclr){
num1=0;
num2=0;
check=0;
xd=0;
z="";
l1.setText(z);
}
Here all the variables we use are updated to their default values of 0
and set the label l1 to null so we can start a new calculation later
5. For the backspace button
if(e.getSource()==bback){
zt=l1.getText();
try{
z=zt.substring(0, zt.length()-1);
}catch(StringIndexOutOfBoundsException f){return;}
l1.setText(z);
}
Here just l1 update the value by removing the last digit using the substring function
and handled a StringIndexOutOfBoundsException that occurs when we have a null value in the label and the back button is still pressed
6. Special plug-in function
All I did was handle an exception in EQUAL and all ARITHMETIC Buttons and print the desired message depending on the situation
Math button:
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
Equal to button:
try{
num2=Double.parseDouble(l1.getText());
}catch(Exception f){
l1.setText("ENTER NUMBER FIRST ");
return;
}
When we convert the value to double, but lets say, label l1 has null value (ie label is empty) and we still press these buttons, then it will generate NumberFormatException execption, so process and print the desired message.
==Example==:
if i click 1 then + then i click - instead of some other number button so it's an invalid format and when - is clicked then the label is empty so the execption is generated so just process it and print the invalid format in the label
Similarly when the label is empty and in this case click = ENTER NUMBER FIRST will show inside the label
So far, we have concluded this Java AWT tutorial.
GIF demo
Complete code attached:
import java.awt.*;
import java.awt.event.*;
class MyCalc extends WindowAdapter implements ActionListener{
Frame f;
Label l1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button badd,bsub,bmult,bdiv,bmod,bcalc,bclr,bpts,bneg,bback;
double xd;
double num1,num2,check;
MyCalc(){
f= new Frame("MY CALCULATOR");
// 实例化组件
l1=new Label();
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(50,50,260,60);
b1=new Button("1");
b1.setBounds(50,340,50,50);
b2=new Button("2");
b2.setBounds(120,340,50,50);
b3=new Button("3");
b3.setBounds(190,340,50,50);
b4=new Button("4");
b4.setBounds(50,270,50,50);
b5=new Button("5");
b5.setBounds(120,270,50,50);
b6=new Button("6");
b6.setBounds(190,270,50,50);
b7=new Button("7");
b7.setBounds(50,200,50,50);
b8=new Button("8");
b8.setBounds(120,200,50,50);
b9=new Button("9");
b9.setBounds(190,200,50,50);
b0=new Button("0");
b0.setBounds(120,410,50,50);
bneg=new Button("+/-");
bneg.setBounds(50,410,50,50);
bpts=new Button(".");
bpts.setBounds(190,410,50,50);
bback=new Button("back");
bback.setBounds(120,130,50,50);
badd=new Button("+");
badd.setBounds(260,340,50,50);
bsub=new Button("-");
bsub.setBounds(260,270,50,50);
bmult=new Button("*");
bmult.setBounds(260,200,50,50);
bdiv=new Button("/");
bdiv.setBounds(260,130,50,50);
bmod=new Button("%");
bmod.setBounds(190,130,50,50);
bcalc=new Button("=");
bcalc.setBounds(245,410,65,50);
bclr=new Button("CE");
bclr.setBounds(50,130,65,50);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bpts.addActionListener(this);
bneg.addActionListener(this);
bback.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);
bmod.addActionListener(this);
bcalc.addActionListener(this);
bclr.addActionListener(this);
f.addWindowListener(this);
//添加到框架
f.add(l1);
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);f.add(b6); f.add(b7); f.add(b8);f.add(b9);f.add(b0);
f.add(badd); f.add(bsub); f.add(bmod); f.add(bmult); f.add(bdiv); f.add(bmod);f.add(bcalc);
f.add(bclr); f.add(bpts);f.add(bneg); f.add(bback);
f.setSize(360,500);
f.setLayout(null);
f.setVisible(true);
}
//关闭窗口
public void windowClosing(WindowEvent e) {
f.dispose();
}
public void actionPerformed(ActionEvent e){
String z,zt;
//数字按钮
if(e.getSource()==b1){
zt=l1.getText();
z=zt+"1";
l1.setText(z);
}
if(e.getSource()==b2){
zt=l1.getText();
z=zt+"2";
l1.setText(z);
}
if(e.getSource()==b3){
zt=l1.getText();
z=zt+"3";
l1.setText(z);
}
if(e.getSource()==b4){
zt=l1.getText();
z=zt+"4";
l1.setText(z);
}
if(e.getSource()==b5){
zt=l1.getText();
z=zt+"5";
l1.setText(z);
}
if(e.getSource()==b6){
zt=l1.getText();
z=zt+"6";
l1.setText(z);
}
if(e.getSource()==b7){
zt=l1.getText();
z=zt+"7";
l1.setText(z);
}
if(e.getSource()==b8){
zt=l1.getText();
z=zt+"8";
l1.setText(z);
}
if(e.getSource()==b9){
zt=l1.getText();
z=zt+"9";
l1.setText(z);
}
if(e.getSource()==b0){
zt=l1.getText();
z=zt+"0";
l1.setText(z);
}
if(e.getSource()==bpts){ //添加小数点
zt=l1.getText();
z=zt+".";
l1.setText(z);
}
if(e.getSource()==bneg){ //对于减
zt=l1.getText();
z="-"+zt;
l1.setText(z);
}
if(e.getSource()==bback){ // 退格用
zt=l1.getText();
try{
z=zt.substring(0, zt.length()-1);
}catch(StringIndexOutOfBoundsException f){return;}
l1.setText(z);
}
//算术按钮
if(e.getSource()==badd){ //对应加法
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=1;
}
if(e.getSource()==bsub){ //对应减法
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=2;
}
if(e.getSource()==bmult){ //对应乘法
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=3;
}
if(e.getSource()==bdiv){ //对应除法
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=4;
}
if(e.getSource()==bmod){ //对应MOD/剩余
try{
num1=Double.parseDouble(l1.getText());
}catch(NumberFormatException f){
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=5;
}
//结果按钮
if(e.getSource()==bcalc){
try{
num2=Double.parseDouble(l1.getText());
}catch(Exception f){
l1.setText("ENTER NUMBER FIRST ");
return;
}
if(check==1)
xd =num1+num2;
if(check==2)
xd =num1-num2;
if(check==3)
xd =num1*num2;
if(check==4)
xd =num1/num2;
if(check==5)
xd =num1%num2;
l1.setText(String.valueOf(xd));
}
//清除标签和内存
if(e.getSource()==bclr){
num1=0;
num2=0;
check=0;
xd=0;
z="";
l1.setText(z);
}
}
//实例化 MyCalc 对象的 main 方法
public static void main(String args[]){
new MyCalc();
}
}
Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~