在显示 jframe 时隐藏单选按钮
Hide radio button on show jframe
我正在使用 netbeans 8。我有 2 个单选按钮,我想在显示框架时隐藏它们。我怎样才能做到这一点?当我点击其他按钮时,我成功地做到了这一点:
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jRadioButton3.setVisible(false);
jRadioButton4.setVisible(false);
}
但这不是我想要的。我想将它设置为不可见,并且仅在单击其他单选按钮时显示它。出于某种原因,netbean 阻止我编辑源代码中的某些区域,因此我无法测试或探索它。请帮助并提前致谢。
将JRadioButton
setVisible
方法默认设置为false
,然后在执行操作时更改它。
例如,在下面,一旦选择了第一个 JRadioButton
,JRadioButtons
就会可见。如果取消选择,它们就会消失。
我是用 JRadioButton
做的,当然也可以用其他组件来做。
解决方案
public class Test extends JFrame{
private JRadioButton but1, but2, but3;
public Test(){
setSize(new Dimension(200,200));
initComp();
setVisible(true);
}
private void initComp() {
but1 = new JRadioButton();
but1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
but2.setVisible(but1.isSelected());
but3.setVisible(but1.isSelected());
}
});
but2 = new JRadioButton();
but2.setVisible(false);
but3 = new JRadioButton();
but3.setVisible(false);
setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.add(but1);
pan.add(but2);
pan.add(but3);
getContentPane().add(pan);
}
public static void main(String[] args) {
new Test();
}
}
您可以在将单选按钮添加到框架时将其设置为不可见,并使其在某些事件中可见:
public class InvisibleRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame();
final JRadioButton jRadioButton1 = new JRadioButton("1");
JRadioButton jRadioButton2 = new JRadioButton("2");
frame.setLayout(new FlowLayout());
frame.add(jRadioButton1);
frame.add(jRadioButton2);
frame.setVisible(true);
jRadioButton1.setVisible(false); // initialize as invisible
jRadioButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jRadioButton1.setVisible(true); // set it to be visible
}
});
frame.pack();
}
}
我正在使用 netbeans 8。我有 2 个单选按钮,我想在显示框架时隐藏它们。我怎样才能做到这一点?当我点击其他按钮时,我成功地做到了这一点:
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jRadioButton3.setVisible(false);
jRadioButton4.setVisible(false);
}
但这不是我想要的。我想将它设置为不可见,并且仅在单击其他单选按钮时显示它。出于某种原因,netbean 阻止我编辑源代码中的某些区域,因此我无法测试或探索它。请帮助并提前致谢。
将JRadioButton
setVisible
方法默认设置为false
,然后在执行操作时更改它。
例如,在下面,一旦选择了第一个 JRadioButton
,JRadioButtons
就会可见。如果取消选择,它们就会消失。
我是用 JRadioButton
做的,当然也可以用其他组件来做。
解决方案
public class Test extends JFrame{
private JRadioButton but1, but2, but3;
public Test(){
setSize(new Dimension(200,200));
initComp();
setVisible(true);
}
private void initComp() {
but1 = new JRadioButton();
but1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
but2.setVisible(but1.isSelected());
but3.setVisible(but1.isSelected());
}
});
but2 = new JRadioButton();
but2.setVisible(false);
but3 = new JRadioButton();
but3.setVisible(false);
setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.add(but1);
pan.add(but2);
pan.add(but3);
getContentPane().add(pan);
}
public static void main(String[] args) {
new Test();
}
}
您可以在将单选按钮添加到框架时将其设置为不可见,并使其在某些事件中可见:
public class InvisibleRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame();
final JRadioButton jRadioButton1 = new JRadioButton("1");
JRadioButton jRadioButton2 = new JRadioButton("2");
frame.setLayout(new FlowLayout());
frame.add(jRadioButton1);
frame.add(jRadioButton2);
frame.setVisible(true);
jRadioButton1.setVisible(false); // initialize as invisible
jRadioButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jRadioButton1.setVisible(true); // set it to be visible
}
});
frame.pack();
}
}