首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JTextField单击JRadioButton时设置JRadioButton可见

在JTextField单击JRadioButton时设置JRadioButton可见
EN

Stack Overflow用户
提问于 2015-11-30 10:08:02
回答 1查看 1.1K关注 0票数 1

我正在Netbeans中创建一个GUI,我希望在选择单选按钮时设置一个文本字段。由于某些原因,检测到单选按钮单击,但选定时不显示文本字段。对于如何处理这个问题,有什么建议吗?我的代码粘贴在下面。文本字段称为newContainerNameInput,单选按钮为newContainerRadioButton:

代码语言:javascript
复制
    containersButtonGroup.add(newContainerRadioButton);
    newContainerRadioButton.setText("Create a new container");
    newContainerRadioButton.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            newContainerRadioButtonItemStateChanged(evt);
        }
    });
    newContainerRadioButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newContainerRadioButtonActionPerformed(evt);
        }
    });

    newContainerNameInput.setText("Enter new container name here");
    newContainerNameInput.setVisible(false);
代码语言:javascript
复制
private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    System.out.println("test");
    newContainerNameInput.setVisible(true);
}  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-30 10:46:45

替换ActionListener of RadioButton中的下列代码

代码语言:javascript
复制
private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
  System.out.println("test");
  newContainerNameInput.setVisible(true);
  revalidate();
} 

revalidate()正在做两件事。第一个是invalidate()validate()。通过这样做,您的组件将被标记为无效并再次验证。这意味着再次布局..。有关更多信息,请参见javadoc

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33995980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档