This article is from huawei cloud community “How to use Java AWT to create a Simple calculator”, author: Hai Yong.

About the AWT

AWT (Abstract Window Toolkit) is an API (graphical user interface) that helps build guIs based on Java applications. The GUI uses graphics to help users interact. It consists mainly of a set of classes and methods required, such as GUI buttons, Windows, frames, text boxes, radio buttons, and so on, created and managed in a simplified manner.

The Java code I’ve provided is a calculator for event handling for the action listener interface.

The logical part

1. For numeric buttons

Zt = l1.gettext (); if(LLDB etSource()==b1){LLDB = l1.gettext (); z=zt+"1"; // 1 will be merged at the end of the previous value l1.settext (z); }Copy the code

When any number button is pressed, any value in label L1 is stored in the variable ZT, then concatenated with the corresponding number and displayed in label L1, similar processing is done for NEGATIVE and DECIMAL PTS buttons.

2. For arithmetic buttons

If (LLDB etSource()==badd){// num1=Double. ParseDouble (l1.gettext ()); z=""; l1.setText(z); check=1; }Copy the code

Now, after converting the value of the label L1 to a double, we store it in the variable num1, which is technically the first number, and set the label L1 to NULL.

We’ll just use a check variable to get that particular pneumatic button (here +) is clicked, so we can perform this operation in our = button.

3. For the equal sign 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));
}
Copy the code

Now store the value l1 again into the num2 variable, which will be the second number in arithmetic, then check the value of the variable, check then do the corresponding operation, and then display the result L1 in the label.

4. For clear button

 if(e.getSource()==bclr){
  num1=0;
  num2=0;
  check=0;
  xd=0;
   z="";
   l1.setText(z);
   } 
Copy the code

Here we update all the variables we use to their default value of 0.

And set the label L1 to NULL so we can start a new calculation later.

5. For backspace buttons

if(e.getSource()==bback){ zt=l1.getText(); try{ z=zt.substring(0, zt.length()-1); }catch(StringIndexOutOfBoundsException f){return; } l1.setText(z); }Copy the code

Here l1 simply updates the value by removing the last digit using the substring function

And processing a StringIndexOutOfBoundsException when we are in the tag value is null and still there was an exception to press the back button.

6. Special plug-in functions

All I did was handle an exception in EQUAL and all of the ARITHMETIC Buttons, and print the required message as appropriate

Arithmetic button:

try{
    num1=Double.parseDouble(l1.getText());
    }catch(NumberFormatException f){
      l1.setText("Invalid Format");
      return;
    }
Copy the code

Equal to button:

try{
    num2=Double.parseDouble(l1.getText());
    }catch(Exception f){
      l1.setText("ENTER NUMBER FIRST ");
      return;
    }
Copy the code

When we convert the value to a double value, but so to speak, the label L1 has a null value (that is, the label is empty) and we still press these buttons, then it will generate the NumberFormatException execption, so it processes and prints the desired message.

== For example == :

If I click 1 and then + and then I click – instead of some other numeric button, so this is an invalid format, and the label is empty when – is clicked so the execption is generated so it just processes it and prints the invalid format in the label.

Similarly, when the label is empty, and in this case clicking = ENTER NUMBER FIRST will be displayed within the label.

This concludes the 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"); // instantiate component l1=new Label(); l1.setBackground(Color.LIGHT_GRAY); L1. SetBounds (50,50,260,60); b1=new Button("1"); B1. SetBounds (50340,50,50); b2=new Button("2"); B2. SetBounds (120340,50,50); b3=new Button("3"); B3. SetBounds (190340,50,50); b4=new Button("4"); B4. SetBounds (50270,50,50); b5=new Button("5"); B5. SetBounds (120270,50,50); b6=new Button("6"); B6. SetBounds (190270,50,50); b7=new Button("7"); B7. SetBounds (50200,50,50); b8=new Button("8"); B8. SetBounds (120200,50,50); b9=new Button("9"); B9. SetBounds (190200,50,50); b0=new Button("0"); B0. SetBounds (120410,50,50); bneg=new Button("+/-"); Bneg. SetBounds (50410,50,50); bpts=new Button("."); BPTS. SetBounds (190410,50,50); bback=new Button("back"); Bback. SetBounds (120130,50,50); badd=new Button("+"); Badd. SetBounds (260340,50,50); bsub=new Button("-"); Bsub. SetBounds (260270,50,50); bmult=new Button("*"); Bmult. SetBounds (260200,50,50); bdiv=new Button("/"); Bdiv. SetBounds (260130,50,50); bmod=new Button("%"); Bmod. SetBounds (190130,50,50); bcalc=new Button("="); Bcalc. SetBounds (245410,65,50); bclr=new Button("CE"); BCLR. SetBounds (50130,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); // Add to frame f.dd (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.s etSize (360500); f.setLayout(null); f.setVisible(true); } public void windowClosing(WindowEvent e) {f.dipose (); } public void actionPerformed(ActionEvent e){ String z,zt; If (LLDB etSource()==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); } the if (um participant etSource () = = BPTS) {/ / add decimal zt = l1. The getText (); z=zt+"."; l1.setText(z); } the if (um participant etSource () = = bneg) {/ / for zt loss = l1. GetText (); z="-"+zt; l1.setText(z); } the if (um participant etSource () = = bback) {/ / backspace zt = l1 getText (); try{ z=zt.substring(0, zt.length()-1); }catch(StringIndexOutOfBoundsException f){return; } l1.setText(z); {LLDB = LLDB (LLDB){LLDB = LLDB (LLDB){LLDB = LLDB (LLDB){LLDB = LLDB (LLDB); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=1; } if(LLLLDB ()==bsub){num1=Double (llll.gettext ()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=2; } if(LLDB etSource()==bmult){num1=Double. ParseDouble (l1.gettext); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=3; } if(LLLLDB ()==bdiv){num1=Double (llll.gettext ()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=4; } if(LLLLDB ()==bmod){num1=Double (llll.gettext ()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=5; } if(LLDB etSource()==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(LLDB etSource()== BCLR){num1=0; num2=0; check=0; xd=0; z=""; l1.setText(z); Public static void main(String args[]){new MyCalc(); }}Copy the code

Click to follow, the first time to learn about Huawei cloud fresh technology ~