简体   繁体   English

将JTextField设置为在JRadioButton上可见单击

[英]Setting JTextField visible on JRadioButton Click

I'm creating a GUI in Netbeans and I want to set a text field to appear when a radio button is selected. 我正在Netbeans中创建一个GUI,我想设置一个文本字段,使其在选择单选按钮时显示。 For some reason, the radio button click is detected, but the text field does not appear upon selection. 出于某种原因,单选按钮单击被检测到,但选择时未显示文本字段。 Any suggestions on how to handle this problem? 有关如何处理此问题的任何建议? My code is pasted below. 我的代码粘贴在下面。 The text field is called newContainerNameInput, and the radio button is newContainerRadioButton: 文本字段称为newContainerNameInput,单选按钮为newContainerRadioButton:

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

private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    System.out.println("test");
    newContainerNameInput.setVisible(true);
}  

Replace following code in the ActionListener of RadioButton RadioButtonActionListener中替换以下代码

private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
  System.out.println("test");
  newContainerNameInput.setVisible(true);
  revalidate();
} 

revalidate() is doing 2 things. revalidate()做2件事。 First invalidate() and validate() . 首先是invalidate()validate() By doing this your components get marked invalid and validated again. 这样,您的组件将被标记为无效并再次进行验证。 That means layout again.. For more see javadoc 这意味着再次布局。有关更多信息,请参见javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM