I am getting an error in my actionlistener on my setText() method because it isn't defined with a string

StackOverflow https://stackoverflow.com/questions/19431596

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:

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");
            }
        }
    }
有帮助吗?

解决方案

You just have to change

result = Double.toString(z).setText();

to

result.setText(Double.toString(z));

setText() is the a method on your JTextField and takes a parameter of what you want to set the JTextField to as a string. By passing in your Double.toString(z) you will set what text is stored in the JTextField to the value of the Double z.

If you're looking for some further reading here's the official documentation

其他提示

result = Double.toString(z).setText(); is the issue

You are attempting to set text for a JLabel. If you need to set the text for JLabel you need to do the following:

result.setText(Double.toString(z));  

and your problem is solved. Add a main() and here you go:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top