我的目的是让用户单击 10 个按钮并让它在不可编辑的 JTextField 中显示总和。我在控制台中做了一些测试,以表明它计算正确,并且我的成功是:当我单击数字时,控制台将它们相加。 (/image/6t4GD.jpg)
我知道你不能在 JTextField 中输入 int 值,所以我尝试使用 String.valueOf();对其进行转换并正确输出。控制台一直显示为 0。也许它需要在其他地方重新激活?我不知道。我也不相信当我单击按钮时它会发生变化。我认为这不是它的工作原理,但我不知道会发生什么,所以我在这里。
所以我的问题是:
1)我在将总计转换为 sTotal 时做错了什么
2)如果即使有一个actionevent,数字也不会改变,我有什么选择?
提前感谢您,这是来源!
编辑:我通过添加解决了字符串转换的问题
String sTotal = String.valueOf(total);
System.out.println(sTotal);
底部的system.out.println.(total);
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
“值”,而不是对其的引用。 sTotal 将保持为“0”,直到您更新它,并且除了实例化之外没有证据表明它被更新。即使更新了 sTotal,该组件也不会更新,因为传递的是 String 值,而不是对该值的引用。
关于java - 动态更改 JTextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117433/