实验十三:窗口设计

package 自我介绍;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Interduction extends JFrame implements ComponentListener,ActionListener{
private JRadioButton[] radios;
private JComboBox<String> province,city,profession,college;
private static String[] provinces= {"海南","青海"};
private static String[][] professional= {{"网络工程","物联网工程","计算机科学与技术"},{"化学","应用化学"}};
private static String[] colleges= {"计算机学院","化学化工学院"};
private static String[][] cities= {{"海口","万宁","三亚"},{"西宁","海东","海西"}};
public Interduction(){
this.setBounds(300,240,360,120);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addComponentListener(this);

//输入姓名
this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
this.getContentPane().add(new JLabel("姓名",JLabel.CENTER));
this.getContentPane().add(new JTextField("李会通"));

//选择性别
String[] sex= {"男","女"};
JPanel jp=new JPanel(new GridLayout(1,2));
this.add(jp);
ButtonGroup bg=new ButtonGroup();
this.radios=new JRadioButton[sex.length];
for(int i=0;i<this.radios.length;i++) {
jp.add(this.radios[i]=new JRadioButton(sex[i]));
bg.add(this.radios[i]);
}
//选择省份
this.getContentPane().add(new JLabel("籍贯",JLabel.CENTER));
this.add(this.province=new JComboBox<String>(Interduction.provinces));
this.add(this.city=new JComboBox<String>(Interduction.cities[0]));
this.province.addActionListener(this);

//输入学号
this.getContentPane().add(new JLabel("学号",JLabel.CENTER));
this.getContentPane().add(new JTextField("20173311102"));

//选择学院专业
this.getContentPane().add(new JLabel("学院",JLabel.CENTER));
this.add(this.college=new JComboBox<String>(Interduction.colleges));
this.getContentPane().add(new JLabel("专业",JLabel.CENTER));
this.add(this.profession=new JComboBox<String>(Interduction.professional[0]));
this.province.addActionListener(this);
//可视化
this.setVisible(true);

}
//动作事件处理
public void actionPerformed(ActionEvent event) {
int i=this.province.getSelectedIndex();
if(cities!=null&&i!=1) {
this.city.removeAllItems();
for(int j=0;j<Interduction.cities[i].length;j++)
this.city.addItem(Interduction.cities[i][j]);
}
int k=this.college.getSelectedIndex();
if(professional!=null&&k!=1) {
this.profession.removeAllItems();
for(int l=0;l<Interduction.professional[k].length;l++)
this.city.addItem(Interduction.professional[k][l]);
}

public static void main(String[] args) {
new Interduction();

}

}

 

实验结果:

posted @ 2019-06-23 20:02  ◉‿◉  阅读(100)  评论(0编辑  收藏  举报