GUI编程——单选框、复选框以及多选框

2021/4/15 22:55:08

本文主要是介绍GUI编程——单选框、复选框以及多选框,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

单选框

 1 package com.kuang.lesson05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.net.URL;
 6 
 7 public class JButtonDemo02 extends JFrame {
 8 
 9     public JButtonDemo02(){
10         Container container=this.getContentPane();
11         //将一个图片变为图标
12         URL resource=JButtonDemo01.class.getResource("332556.jpg");
13         Icon icon=new ImageIcon(resource);
14 
15         //单选框
16         JRadioButton radioButton1=new JRadioButton("JRadioButton01");
17         JRadioButton radioButton2=new JRadioButton("JRadioButton02");
18         JRadioButton radioButton3=new JRadioButton("JRadioButton03");
19 
20         //由于单选框只能选择一个,分组,一个组中只能选择一个
21         ButtonGroup group=new ButtonGroup();
22         group.add(radioButton1);
23         group.add(radioButton2);
24         group.add(radioButton3);
25 
26         container.add(radioButton1,BorderLayout.CENTER);
27         container.add(radioButton2,BorderLayout.NORTH);
28         container.add(radioButton3,BorderLayout.SOUTH);
29 
30         this.setVisible(true);
31         this.setSize(500,300);
32         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
33     }
34 
35     public static void main(String[] args) {
36         new JButtonDemo02();
37     }
38 }

 

复选框

 1 package com.kuang.lesson05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.net.URL;
 6 
 7 public class JButtonDemo02 extends JFrame {
 8 
 9     public JButtonDemo02(){
10         Container container=this.getContentPane();
11         //将一个图片变为图标
12         URL resource=JButtonDemo01.class.getResource("332556.jpg");
13         Icon icon=new ImageIcon(resource);
14 
15         //单选框
16         JRadioButton radioButton1=new JRadioButton("JRadioButton01");
17         JRadioButton radioButton2=new JRadioButton("JRadioButton02");
18         JRadioButton radioButton3=new JRadioButton("JRadioButton03");
19 
20         container.add(radioButton1,BorderLayout.CENTER);
21         container.add(radioButton2,BorderLayout.NORTH);
22         container.add(radioButton3,BorderLayout.SOUTH);
23 
24         this.setVisible(true);
25         this.setSize(500,300);
26         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
27     }
28 
29     public static void main(String[] args) {
30         new JButtonDemo02();
31     }
32 }

 

多选框

 1 package com.kuang.lesson05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.net.URL;
 6 
 7 public class JButtonDemo03 extends JFrame {
 8 
 9     public JButtonDemo03(){
10         Container container=this.getContentPane();
11         //将一个图片变为图标
12         URL resource=JButtonDemo01.class.getResource("332556.jpg");
13         Icon icon=new ImageIcon(resource);
14 
15         //多选框
16         JCheckBox checkBox01=new JCheckBox("checkBox01");
17         JCheckBox checkBox02=new JCheckBox("checkBox02");
18 
19         container.add(checkBox01,BorderLayout.NORTH);
20         container.add(checkBox02,BorderLayout.SOUTH);
21 
22         this.setVisible(true);
23         this.setSize(500,300);
24         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
25     }
26 
27     public static void main(String[] args) {
28         new JButtonDemo03();
29     }
30 }

 



这篇关于GUI编程——单选框、复选框以及多选框的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程