简体   繁体   中英

Issues with buttons in javas calculator

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class H4 extends Applet
  implements ActionListener{
    float f1, s1 ,sum1, f2, s2, sum2, f3, s3, sum3, f4, s4, sum4;                 
    TextField a1, b1, eq1, a2, b2, eq2, a3, b3, eq3, a4, b4, eq4;
    Button equal1, equal2, equal3, equal4;

  public void init() {
      resize(800,600);
  
      equal1 = new Button("=");
      equal2 = new Button("=");
      equal3 = new Button("=");
      equal4 = new Button("=");
      
      a1 = new TextField(12);
      b1 = new TextField(12);
      eq1 = new TextField(12);
      
      a2 = new TextField(12);
      b2 = new TextField(12);
      eq2 = new TextField(12);
      
      a3 = new TextField(12);
      b3 = new TextField(12);
      eq3 = new TextField(12);
      
      a4 = new TextField(12);
      b4 = new TextField(12);
      eq4 = new TextField(12);
      
      Label plus = new Label("+");
      Label minus = new Label("-");
      Label multiply = new Label("*");
      Label divide = new Label("/");
      
      
      setLayout(null);
      
      a1.setBounds(60, 20, 40, 20);
      b1.setBounds(120, 20, 40, 20);
      eq1.setBounds(190, 20, 40, 20);
      equal1.setBounds(165, 19, 20, 20);
      plus.setBounds(103, 20, 40, 20);
      
      a2.setBounds(60, 50, 40, 20);
      b2.setBounds(120, 50, 40, 20);
      eq2.setBounds(190, 50, 40, 20);
      equal2.setBounds(165, 49, 20, 20);
      minus.setBounds(103, 50, 40, 20);
      
      a3.setBounds(60, 80, 40, 20);
      b3.setBounds(120, 80, 40, 20);
      eq3.setBounds(190, 80, 40, 20);
      equal3.setBounds(165, 79, 20, 20);
      multiply.setBounds(103, 80, 40, 20);
      
      a4.setBounds(60, 110, 40, 20);
      b4.setBounds(120, 110, 40, 20);
      eq4.setBounds(190, 110, 40, 20);
      equal4.setBounds(165, 109, 20, 20);
      divide.setBounds(103, 110, 40, 20);
      
      add(a1);
      add(b1);
      add(equal1);
      add(eq1);
      add(plus);
      
      add(a2);
      add(b2);
      add(equal2);
      add(eq2);
      add(minus);
      
      add(a3);
      add(b3);
      add(equal3);
      add(eq3);
      add(multiply);
      
      add(a4);
      add(b4);
      add(equal4);
      add(eq4);
      add(divide);
      
      equal1.addActionListener(this);
      equal2.addActionListener(this);
      equal3.addActionListener(this);
      equal4.addActionListener(this);
    
  }
  public void actionPerformed(ActionEvent e) {
      if(a1.getText()!= null || b1.getText()!= null){
      f1 = Float.valueOf(a1.getText()).floatValue();
      s1 = Float.valueOf(b1.getText()).floatValue();
      sum1 = f1 + s1;
      eq1.setText(Float.toString(sum1));
      }
      else if (a2.getText()!= null || b2.getText()!= null){
      f2 = Float.valueOf(a2.getText()).floatValue();
      s2 = Float.valueOf(b2.getText()).floatValue();
      sum2 = f2 - s2;
      eq2.setText(Float.toString(sum2));
      }
      else if (a3.getText()!= null || b3.getText()!= null) {
      f3 = Float.valueOf(a3.getText()).floatValue();
      s3 = Float.valueOf(b3.getText()).floatValue();
      sum3 = f3 * s3;
      eq3.setText(Float.toString(sum3));
      }
      else{
      f4 = Float.valueOf(a4.getText()).floatValue();
      s4 = Float.valueOf(b4.getText()).floatValue();
      sum4 = f4 / s4;
      eq4.setText(Float.toString(sum4));
      }
}}  

The problem is that when i click on the button are activated all 4 buttons, i have tried to fix that, but i could do that. I tried to get text from testfiels and check if it doesnt equal null but programm works only with first button others doesnt work at all. Can someone help me with that problem? I m using old librarry applets because im studing in the university, and my teacher is old and she doesnt even now new versions of java she is just gaving us exercises with code examples thats what i try to do.

Here are two solutions:

(1) You can check which action (button) triggered the event with ActionEvent#getSource() . This method is inherited from EventObject and the docs read:

public Object getSource()

The object on which the Event initially occurred.

Returns: The object on which the Event initially occurred.

Example:

public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();

  if(source.equals(equals1) {
    // Button "equals1" was clicked.
  } else if(source.equals(equals2)) {
    // Button "equals2" was clicked.
  } else if...
}

(2) Use separate methods for each button.


equals1.addActionListener(this::equals1Clicked);
equals2.addActionListener(this::equals2Clicked);
...

public void equals1Clicked(ActionEvent e) {
  // Button "equals1" was clicked.
}

public void equals2Clicked(ActionEvent e) {
  // Button "equals2" was clicked.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM