【问题标题】:Dynamically changing the title of a final JRadioButton动态更改最终 JRadioButton 的标题
【发布时间】:2014-04-03 08:25:08
【问题描述】:

我遇到了这个问题,我不知道如何更新我的 JRadioButtons 的名称

我已经像这样在contsructor中设置了它们:

final JRadioButton ANSWER1 = new JRadioButton(answer1);

其中 answer1 是一个字符串。

但每当我更改 answer1 时,JRadioButton 的名称不会改变。

我已将 JRadioButton 设置为在单击 JButton 时更改名称:

NEXT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
    qnumber++;
    answer1 = "blah blah";
    ANSWER1.setText(answer1);

但这似乎没有任何效果,任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 听起来您遇到了阴影或上下文问题...
  • 有点初学者,什么意思?
  • @chrispbacon answer1 的数据类型是什么?
  • 你能发布完整的代码吗?另外,请确保通过放置断点进入 actionPerformed 方法。
  • 你认为你正在改变的是你真正想要改变的

标签: java swing jframe jbutton jradiobutton


【解决方案1】:
final JRadioButton ANSWER1 = new JRadioButton(answer1);

应该是(搜索 Java 命名约定)

final JRadioButton answer1 = new JRadioButton(ANSWER1);

  • String value answer1中隐藏的任何东西(这个变量应该定义为constant - private String ANSWER1)可以用于setNamesetActionCommandputClientPropertySwing Action的描述符,你可以返回这个每个 AWT/Swing Listeners 的值添加到 JRadioButton

  • 使用JLabel在屏幕上显示叙述,您可以使用setLabelFor链接JLabelJRadioButton

  • 使用ItemListener 代替JRadioButton,测试SELECTED/DESELECTED 而不是ActionListener

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2015-05-21
    • 2017-03-13
    • 2020-01-04
    相关资源
    最近更新 更多