如何在Java中更新同一文本字段上的数组?

huangapple go评论71阅读模式
英文:

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 &lt; 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);

huangapple
  • 本文由 发表于 2020年9月30日 02:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定