3.5 按钮
-
单选按钮
package GUI.Swing; import javax.swing.*; import java.awt.*; public class JButtonDemo extends JFrame { public JButtonDemo(){ Container container = this.getContentPane(); //单选框 JRadioButton radioButton01 = new JRadioButton("radioButton01"); JRadioButton radioButton02 = new JRadioButton("radioButton02"); JRadioButton radioButton03 = new JRadioButton("radioButton03"); //由于单选框不能同时多个选择 分组,一个组中只能选择一个 ButtonGroup group = new ButtonGroup(); group.add(radioButton01); group.add(radioButton02); group.add(radioButton03); container.add(radioButton01,BorderLayout.NORTH); container.add(radioButton02,BorderLayout.CENTER); container.add(radioButton03,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,200,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo(); } }
-
复选按钮
package GUI.Swing; import javax.swing.*; import java.awt.*; public class JButtonDemo2 extends JFrame { public JButtonDemo2(){ Container container = this.getContentPane(); //多选框 JCheckBox checkBox01 = new JCheckBox("checkBox01"); JCheckBox checkBox02 = new JCheckBox("checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,200,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo2(); } }