码迷,mamicode.com
首页 > Windows程序 > 详细

实验十二:SWING界面设计

时间:2019-06-17 20:42:03      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:工程   tco   get   items   text   ring   import   实验代码   http   

实验代码:

 1 package ji;
 2 
 3 import java.awt.FlowLayout;
 4 import java.awt.GridLayout;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 import java.awt.event.ComponentListener;
 8 
 9 import javax.swing.ButtonGroup;
10 import javax.swing.JComboBox;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JRadioButton;
15 import javax.swing.JTextField;
16 
17 public class Ji extends JFrame implements ComponentListener,ActionListener{
18 private JRadioButton[] radios;
19 private JComboBox<String> province,city,profession,college;
20 private static String[] provinces= {"山东","枣庄"};
21 private static String[][] professional= {{"网络工程","物联网工程","计算机科学与技术"},{"社会学","社会工作学"}};
22 private static String[] colleges= {"计算机学院","法社学院"};
23 private static String[][] cities= {{"济南","青岛","枣庄"},{"西宁","海东","海西"}};
24 public Ji(){
25 this.setBounds(300,240,360,120);
26 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
27 this.addComponentListener(this);
28 
29 //输入姓名
30 this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
31 this.getContentPane().add(new JLabel("姓名",JLabel.CENTER));
32 this.getContentPane().add(new JTextField("张灿"));
33 
34 //选择性别
35 String[] sex= {"男","女"};
36 JPanel jp=new JPanel(new GridLayout(1,2));
37 this.add(jp);
38 ButtonGroup bg=new ButtonGroup();
39 this.radios=new JRadioButton[sex.length];
40 for(int i=0;i<this.radios.length;i++) {
41 jp.add(this.radios[i]=new JRadioButton(sex[i]));
42 bg.add(this.radios[i]);
43 }
44 //选择省份
45 this.getContentPane().add(new JLabel("籍贯",JLabel.CENTER));
46 this.add(this.province=new JComboBox<String>(Ji.provinces));
47 this.add(this.city=new JComboBox<String>(Ji.cities[0]));
48 this.province.addActionListener(this);
49 
50 //输入学号
51 this.getContentPane().add(new JLabel("学号",JLabel.CENTER));
52 this.getContentPane().add(new JTextField("20173311102"));
53 
54 //选择学院专业
55 this.getContentPane().add(new JLabel("学院",JLabel.CENTER));
56 this.add(this.college=new JComboBox<String>(Ji.colleges));
57 this.getContentPane().add(new JLabel("专业",JLabel.CENTER));
58 this.add(this.profession=new JComboBox<String>(Ji.professional[0]));
59 this.province.addActionListener(this);
60 //可视化
61 this.setVisible(true);
62 
63 }
64 //动作事件处理
65 public void actionPerformed(ActionEvent event) {
66 int i=this.province.getSelectedIndex();
67 if(cities!=null&&i!=1) {
68 this.city.removeAllItems();
69 for(int j=0;j<Ji.cities[i].length;j++)
70 this.city.addItem(Ji.cities[i][j]);
71 }
72 int k=this.college.getSelectedIndex();
73 if(professional!=null&&k!=1) {
74 this.profession.removeAllItems();
75 for(int l=0;l<Ji.professional[k].length;l++)
76 this.city.addItem(Ji.professional[k][l]);
77 }
78 
79 public static void main(String[] args) {
80 new Ji();
81 
82 }
83 
84 }

实验结果:

技术图片

 

实验十二:SWING界面设计

标签:工程   tco   get   items   text   ring   import   实验代码   http   

原文地址:https://www.cnblogs.com/itsRes/p/11041797.html

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