Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find a button source in AWT (calculator homework)
    primarykey
    data
    text
    <p>We were asked to make a simple GUI calculator, I used getSource() to detect the buttons, cause that's what they taught us. It works by typing the 1st and 2nd value then choosing the operation, it worked. But I made a mistake, because what they want us to do is that the numbers should also be buttons, just like a real calculator. so how do i get the value of each button using getSource()? like when I press button 1 and 2 is equal to 3. heres what ive done</p> <pre><code>import java.awt.*; import java.awt.event.*; public class SimpleCalculator implements ActionListener { // containers private Frame f; private Panel p1, p2, p3, p4; // components private Label l1, l2, l3; private TextField tf1, tf2, tf3; private Button bAdd, bSub, bMul, bDiv, bClear; public SimpleCalculator() { f = new Frame("My First GUI App"); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); l1 = new Label("First: "); l2 = new Label("Second: "); l3 = new Label("Result: "); tf1 = new TextField(15); tf2 = new TextField(15); tf3 = new TextField(15); bAdd = new Button("+"); bSub = new Button("-"); bMul = new Button("*"); bDiv = new Button("/"); bClear = new Button("C"); } public void launchFrame() { // use default layout manager of the Panel (FlowLayout) p1.add(l1); p1.add(tf1); p2.add(l2); p2.add(tf2); p3.add(l3); p3.add(tf3); p4.add(bAdd); p4.add(bSub); p4.add(bMul); p4.add(bDiv); p4.add(bClear); // change the layout manager of the Frame, // use GridLayout(4, 1) f.setLayout(new GridLayout(4, 1)); f.add(p1); f.add(p2); f.add(p3); f.add(p4); f.pack(); f.setVisible(true); // register event handlers bAdd.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bClear.addActionListener(this); f.addWindowListener(new MyCloseButtonHandler()); } // override the actionPerformed method public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); double num1, num2, result = 0.0; if (tf1.getText() != null &amp;&amp; tf2.getText() != null) { num1 = Double.parseDouble(tf1.getText()); num2 = Double.parseDouble(tf2.getText()); if (source == bAdd) result = num1 + num2; else if (source == bSub) result = num1 - num2; else if (source == bMul) result = num1 * num2; else if (source == bDiv) result = num1 / num2; else if (source == bClear) { tf1.setText("0.0"); tf2.setText("0.0"); tf3.setText("0.0"); } else {} // tf3.setText(new Double(result).toString()); tf3.setText("" + result); } } private class MyCloseButtonHandler extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } } public static void main(String args[]) { SimpleCalculator sc = new SimpleCalculator(); sc.launchFrame(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload