作者:广东抱米花_冰月 | 来源:互联网 | 2024-10-18 21:25
单选框代码:1packagecom.luckylu.gui;23importjavax.swing.*;4importjava.awt.*;56publicclassJRadioB
单选框
代码:
1 package com.luckylu.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class JRadioButtonDemo extends JFrame {
7 public JRadioButtonDemo() {
8 Container cOntainer= this.getContentPane();
9 //单选框
10 JRadioButton radioButton1 = new JRadioButton("radioButton1");
11 JRadioButton radioButton2 = new JRadioButton("radioButton2");
12 JRadioButton radioButton3 = new JRadioButton("radioButton3");
13
14 //把单选框分到一个组,一个组中实现只能选一个;
15
16 ButtonGroup group = new ButtonGroup();
17 group .add(radioButton1);
18 group .add(radioButton2);
19 group .add(radioButton3);
20
21 container.add(radioButton1,BorderLayout.NORTH);
22 container.add(radioButton2,BorderLayout.CENTER);
23 container.add(radioButton3,BorderLayout.SOUTH);
24
25 this.setVisible(true);
26 this.setTitle("单选框示例");
27 this.setBounds(200,200,300,300);
28 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
29
30 }
31
32 public static void main(String[] args) {
33 new JRadioButtonDemo();
34 }
35
36 }
结果:
复选框
代码
1 package com.luckylu.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class JCheckBoxDemo extends JFrame {
7 public JCheckBoxDemo() {
8 Container cOntainer= this.getContentPane();
9
10 JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
11 JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
12 JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
13
14 container.add(jCheckBox1,BorderLayout.NORTH);
15 container.add(jCheckBox2,BorderLayout.CENTER);
16 container.add(jCheckBox3,BorderLayout.SOUTH);
17
18 this.setVisible(true);
19 this.setTitle("多选框示例");
20 this.setBounds(200,200,300,300);
21 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
22 }
23
24 public static void main(String[] args) {
25 new JCheckBoxDemo();
26 }
27 }
结果