我的setText()方法在actionlistener中出错,因为它没有用字符串定义

时间:2013-10-17 15:57:59

标签: java eclipse swing jtextfield settext

我正在尝试用三个Jtextfield创建一个简单的计算器。前两个保持用户输入,第三个显示计算结果。我已将用户输入转换为字符串以便显示,但显然这是错误的。请帮忙。这是我的代码:

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");
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

你只需要改变

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

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

setText()是您JTextField上的一种方法,并将您要设置JTextField的参数作为字符串。通过传递Double.toString(z),您可以将JTextField中存储的文字设置为Double z的值。

如果您正在寻找进一步的阅读,请访问官方documentation

答案 1 :(得分:1)

result = Double.toString(z).setText();是问题

您正在尝试为JLabel设置文字。如果需要为JLabel设置文本,则需要执行以下操作:

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

你的问题解决了。添加main(),然后转到:

enter image description here