-2

我想要的是:

我有一个 JPanel,上面有一个 JTextarea 和一个 JButton。按下按钮后,我希望在 JTextArea 中显示某个文本(根据 if-else 语句而变化)。

结构:

  1. 我有一个名为的字符串变量yourphone

    String yourphone;

  2. 在里面ActionListenerJButton有 if-else 语句,像这样

    if(R==120) { yourphone = "Galaxy S4"; }
    if(R==130) { yourphone = "Xperia Z ultra"; }

  3. 这就是我的 JTextArea 的样子

    JTextArea txtrphone = new JTextArea(); txtrphone.setText(yourphone); txtrphone.setRows(5); txtrphone.setBounds(68, 84, 285, 148); result.add(txtrphone);

问题

它不工作。

当我单击 时JBUtton,没有任何反应。中没有出现任何内容JTextArea

4

1 回答 1

2

放:

txtrphone.setText(yourphone);

在你的动作监听器中

每当您想要实际更新文本区域的值时,都需要调用 setText(String)。

于 2013-10-23T08:07:16.077 回答