创建 Java Swing - JFrame、JRadioButton、JCheckBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3271567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:05:21  来源:igfitidea点击:

Creating Java Swing - JFrame, JRadioButton, JCheckBox

javaswing

提问by Manu

I am new to java swing. I have pasted my code below for your reference,

我是 Java Swing 的新手。我在下面粘贴了我的代码供您参考,

I am trying to create 2 JRadioButtonsin JFrameand if i click that JRadioButtonit should display 5 JCheckboxesfor each JRadioButtonin the same JFrame.

我想创建2JRadioButtonsJFrame,如果我点击JRadioButton它应该显示5JCheckboxes每一个JRadioButton在同一个JFrame

JRadiobuttonis displaying now but if i click that JRadioButton"JCheckboxes"is not displaying. please see my code below, if any changes need in my code, please do accordingly.i am struggling for this.

JRadiobutton现在正在显示,但如果我点击它JRadioButton"JCheckboxes"不显示。请查看下面我的代码,如果我的代码需要任何更改,请相应地进行。我正在为此而努力。

MultipleFramesExample.javacalling createMainView()in Mainview.javaclass

MultipleFramesExample.java调用createMainView()Mainview.java

public class MultipleFramesExample extends JFrame {

 public  void fun()
 {

  MainView MV = new MainView();
  MV.createMainView();
 }

 public static void main(String[] args) {
   MultipleFramesExample ob=new MultipleFramesExample();
   ob.fun();
 }
}

Mainview.javacreates Jframeand Buttonsetc..

Mainview.java创建JframeButtons等。

    public class MainView extends JFrame implements ActionListener  {

     JFrame frame1;
     MainView mV=null;
     JCheckBox chinButton;
     JRadioButton birdButton;
    MultipleFramesExample ob=new MultipleFramesExample();
  JPanel panel = new JPanel(new BorderLayout());

  public void createMainView() {
  mV = new MainView();

  frame1 = new JFrame();
  frame1.setTitle("Main View");
  frame1.setSize(50,50);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.setVisible(true);
   birdButton = new JRadioButton("click");
  frame1.getContentPane().add(birdButton);
  birdButton.addActionListener(this);

 }

  public void actionPerformed(ActionEvent event) {
    Container contentPane = frame1.getContentPane();
   contentPane.setLayout(new FlowLayout());

    JCheckBox jb=new JCheckBox();
    if (event.getActionCommand().equals(birdButton)) {

   frame1.add(new JCheckBox("JIL1"));
   frame1.add(new JCheckBox("JIL2"));
   frame1.add(new JCheckBox("JIL3"));
   frame1.add(new JCheckBox("JIL4"));
   frame1.add(new JCheckBox("JIL5"));
   frame1.setVisible(true);

   //panel.add(jb, BorderLayout.PAGE_START);

  // panel.getComponentCount();
     }
   }
 public void fun1(){

 }
}

Is it possible to create them like this?

是否可以像这样创建它们?

回答by Peter Lang

Update:Here's a working example:

更新:这是一个工作示例:

Button 1Button 2

按钮 1按钮 2

public class MainView extends JFrame implements ActionListener{
    JRadioButton radioButton1 = new JRadioButton("Button 1");
    JRadioButton radioButton2 = new JRadioButton("Button 2");
    JCheckBox checkBox = new JCheckBox("CheckBox");
    ButtonGroup buttonGroup = new ButtonGroup();

    public MainView() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 100);
        setLayout(new GridLayout());

        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        radioButton1.addActionListener(this);
        radioButton2.addActionListener(this);
        radioButton2.setSelected(true);  // remove to have no button selected

        // ActionListener for CheckBox
        checkBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO: Your action here
            }
        });

        getContentPane().add(radioButton1);
        getContentPane().add(radioButton2);
        getContentPane().add(checkBox);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        checkBox.setVisible(radioButton2.isSelected());
    }

    public static void main(String[] args) {
        new MainView();
    }
}


Original post

原帖

You have some problems with your code:

您的代码有一些问题:

  1. You create new JCheckBoxes whenever the button is clicked. If the user clicks it twice, more checkboxes would be created.
  2. You try to add two checkboxes:
    frame1.add(new JCheckBox("JIL"));
    frame1.getContentPane().add(jb);
  1. JCheckBox每当单击按钮时,您都会创建新的es。如果用户单击它两次,则会创建更多复选框。
  2. 您尝试添加两个复选框:
    frame1.add(new JCheckBox("JIL"));
    frame1.getContentPane().add(jb);

Try the following steps:

尝试以下步骤:

  1. Create and add all objects that you need for testing (JRadioButton, JCheckBox) and make sure that they are both displayed (check Using Layout Managersand A Visual Guide to Layout Managersif you add both but do not see both).

  2. In your ActionListener, use something like checkBox.setVisible(radioButton.isSelected())to switch visibility of your checkBox according to the state of your radioButton.

  1. 创建并添加测试所需的所有对象 ( JRadioButton, JCheckBox) 并确保它们都显示(如果您添加了两个但未看到两者,请检查使用布局管理器布局管理器的视觉指南)。

  2. 在您的 中ActionListener,使用类似checkBox.setVisible(radioButton.isSelected())根据单选按钮状态切换复选框的可见性。