英文:
How to update an array on the same text field on Java?
问题
public void add()
{
int tam = Integer.parseInt(tLength.getText());
String val = (String) procList.getSelectedItem();
String line = t1.getText(); // Get the current content of the text field
for(int i = 0; i < tam; i++)
{
line += val;
}
t1.setText(line);
}
英文:
I want an array of letters based on the desired quantity to be displayed in a text field each time the button is pressed, example: desired quantity: 3, selected letter "A", therefore the layout would be something like this: AAA. After that I want to add another letter with another amount of repeat value, example: letter "D" number of repeats "4", so if the text field contains: AAA it will add letter "D" four times without delete the previous content; AAADDDD.
With the following code, it is possible to add any letter with any repeat value, but when another letter is added, all the old letters are replaced with the new ones.
public void add()
{
int tam = Integer.parseInt(tLength.getText());
String val = (String) procList.getSelectedItem();
String line = (String) procList.getSelectedItem();
for(int i = 0; i < tam - 1; i++)
{
line += val;
}
t1.setText(line);
}
The val
variable is a list where the letters can be choose and the tam
variable is the number of repetitions.
Any suggestion is good.
答案1
得分: 1
> 但是当添加另一个字母时,所有旧字母都会被新字母替换。
t1.setText(line);
setText(...)
方法将会替换现有的文本。您想要追加文本。
一个方法是使用:
t1.setText(t1.getText() + line);
英文:
> but when another letter is added, all the old letters are replaced with the new ones.
t1.setText(line);
The setText(...)
method will replace the existing text. You want to append the text.
One way to do this is to use:
t1.setText(t1.getText() + line);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论