【发布时间】:2014-12-20 18:47:12
【问题描述】:
所以我的 JTextArea 中有一个字符串,这是我的程序的一个简单示例:
int price = 0;
String totalPrice = "Total price is:" + price;
JTextArea outputText = new JTextArea(totalPrice);
outputText.append("\n New Item");
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addPriceButton) {
price += 1;
}
}
每按一次按钮,价格变量就会增加 1。我的问题是如何更新我的 textarea 以反映此更改,因为更多文本已附加到 textarea,因此不能简单地删除它。
【问题讨论】:
-
简单建议将文本移动到文本字段
-
这实际上取决于文本区域中的内容,而不是您的价格。如果它是一个固定的、不太复杂的模板,您可以将其保存在适合
String.format的格式中,并且每次只需使用带有新价格的String.format。如果它是动态的,我们将需要更多信息。 -
你想要做什么真的不是一个好主意