动态更改 JTextField?

Dynamically Change JTextField?

我的目的是让用户单击 10 个按钮并在不可编辑的 JTextField 中显示总和。我在控制台中做了一些测试,以表明它计算正确,而且我的成功是:当我单击它们时,控制台正在累加数字。 ( http://i.imgur.com/Cwyjm0h.jpg )

我知道您不能在 JTextField 中输入 int 值,所以我尝试使用 String.valueOf();转换并正确输出。控制台一直显示它为 0。也许它需要在其他地方重新激活?我不知道。我也不相信当我点击一个按钮时它会发生变化。我认为这不是它的工作原理,但我不知道会发生什么,所以我就在这里。

所以我的问题是:

1) 我在将 total 转换为 sTotal 时做错了什么

2) 如果即使有一个动作事件,数字也不会改变,我有什么选择?

提前致谢,这是来源!

编辑:我通过添加

解决了字符串转换的问题
String sTotal = String.valueOf(total);
    System.out.println(sTotal);

底部 system.out.println.(总计);

import java.awt.FlowLayout;
import java.awt.event.ActionListener;  
import java.awt.event.ActionEvent;
import javax.swing.JFrame; 
import javax.swing.JButton;
import javax.swing.JTextField;

//JFrame builds the window
public class ThreeOne extends JFrame{

int total;
String sTotal = String.valueOf(total);

private JTextField question;
private JButton Button1, Button2, Button3, Button4, Button5, 
Button6, Button7, Button8, Button9, Button10;



//The window
public ThreeOne(){
    super("");
    setLayout(new FlowLayout());

    question = new JTextField(sTotal, 40);

    question.setEditable(false);
    add(question);

    Button1 = new JButton("1");
    add(Button1);
    Button2 = new JButton("2");
    add(Button2);
    Button3 = new JButton("3");
    add(Button3);
    Button4 = new JButton("4");
    add(Button4);
    Button5 = new JButton("5");
    add(Button5);
    Button6 = new JButton("6");
    add(Button6);
    Button7 = new JButton("7");
    add(Button7);
    Button8 = new JButton("8");
    add(Button8);
    Button9 = new JButton("9");
    add(Button9);
    Button10 = new JButton("10");
    add(Button10);





    thehandler handler = new thehandler();
    Button1.addActionListener(handler);
    Button2.addActionListener(handler);
    Button3.addActionListener(handler);
    Button4.addActionListener(handler);
    Button5.addActionListener(handler);
    Button6.addActionListener(handler);
    Button7.addActionListener(handler);
    Button8.addActionListener(handler);
    Button9.addActionListener(handler);
    Button10.addActionListener(handler);


}

//implements means this is the class that handles the events
private class thehandler implements ActionListener{

    public void actionPerformed(ActionEvent event){



        // event is like an enter or a click
        if(event.getSource()== Button1)
        total = total + 1;
        if(event.getSource()== Button2)
            total = total + 2;

        if(event.getSource()== Button3)
            total = total + 3;

        if(event.getSource()== Button4)
            total = total + 4;

        if(event.getSource()== Button5)
            total = total + 5;

        if(event.getSource()== Button6)
            total = total + 6;

        if(event.getSource()== Button7)
            total = total + 7;

        if(event.getSource()== Button8)
            total = total + 8;

        if(event.getSource()== Button9)
            total = total + 9;

        if(event.getSource()== Button10)
            total = total + 10;



    System.out.println(total);




    }

}
      }

您需要在 Actionlistener 中更新它。

就在你的System.out.println

之前
public void actionPerformed(ActionEvent event){

    ....
    question.setText(Integer.toString(total)); //can use "" + total, just need to be a String
    System.out.println(total); //you can use this for sanity checking
}

sTotal 不执行任何操作,因为它是在实例化时创建的,值为 0。

您将 String "value" 传递给应用程序,而不是对它的引用。 sTotal 将保持为“0”,直到您更新它,并且没有证据表明它被更新,除非在实例化时。即使更新了 sTotal,组件也不会更新,因为传递的是字符串值而不是对值的引用。