Note that there are some explanatory texts on larger screens.

plurals
  1. POI am getting an error in my actionlistener on my setText() method because it isn't defined with a string
    text
    copied!<p>I am trying to make a simple calculator with three Jtextfields. the first two hold user input and the third displays the result of the calculation. I have converted my users input into strings so it can be displayed, but obviously that is the wrong thing to do. Please help. Here is my code:</p> <pre><code>public class LetsDoMath extends JFrame { JLabel instruction1; JLabel instruction2; JLabel instruction3; JTextField input1; JTextField input2; JTextField result; JButton plus; JButton minus; JButton times; JButton divides; /** * Constructor of LetsDoMath * */ public LetsDoMath() { setSize(350, 100); setTitle("Some Math Functions"); JPanel panel = new JPanel(); add(panel); instruction1 = new JLabel("First"); panel.add(instruction1); input1 = new JTextField(4); panel.add(input1); instruction2 = new JLabel("Second"); panel.add(instruction2); input2 = new JTextField(4); panel.add(input2); instruction3 = new JLabel("Result"); panel.add(instruction3); result = new JTextField(6); panel.add(result); result.setEditable(false); plus = new JButton("+"); plus.addActionListener(new ButtonListener()); panel.add(plus); minus = new JButton("-"); minus.addActionListener(new ButtonListener()); panel.add(minus); times = new JButton("*"); times.addActionListener(new ButtonListener()); panel.add(times); divides = new JButton("/"); divides.addActionListener(new ButtonListener()); panel.add(divides); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String msg = event.getActionCommand(); String x; String y; double z; x = input1.getText(); System.out.println("Reading " + x + " from the first text field"); System.out.println("Converted value: "+ Double.parseDouble(x)); y = input2.getText(); System.out.println("Reading " + y + " from the first text field"); System.out.println("Converted value: "+ Double.parseDouble(y)); if(event.getSource()== plus) { z = Double.parseDouble(x) + Double.parseDouble(y); System.out.println("Result equals: "+z); result = Double.toString(z).setText(); System.out.println("Ready for next input"); } else if(event.getSource() == minus) { z = Double.parseDouble(x) - Double.parseDouble(y); System.out.println("Result equals: "+z); result = Double.toString(z).setText(); System.out.println("Ready for next input"); } else if(event.getSource() == times) { z = Double.parseDouble(x) * Double.parseDouble(y); System.out.println("Result equals: "+z); result = Double.toString(z).setText(); System.out.println("Ready for next input"); } else if(event.getSource() == divides) { z = Double.parseDouble(x) / Double.parseDouble(y); System.out.println("Result equals: "+z); result = Double.toString(z).setText(); System.out.println("Ready for next input"); } } } </code></pre>
 

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