码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-04-16 12:11:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:java   ui编程   jframe   add   string   end   import   ring   icon   

单选框

 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编程——单选框、复选框以及多选框

标签:java   ui编程   jframe   add   string   end   import   ring   icon   

原文地址:https://www.cnblogs.com/clblogs/p/14664707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!