java之 ------ 图形界面(三)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.TitledBorder; public class UserJFrame extends JFrame implements ActionListener { private int number=1; //编号 private JTextField text_number, text_name; //编号、姓名文本行 private JRadioButton radiob_male, radiob_female; //性别button private Object cities[][]; //存储多省的城市 private JComboBox combox_province, combox_city; //省份、城市组合框 private JButton button_add; //加入button private JTextArea text_user; //文本区 public UserJFrame(Object provinces[], Object cities[][])//參数指定省份和城市数组 { super("输入用户信息"); this.setSize(740, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel rightpanel=new JPanel(new GridLayout(6,1));//右面板 JPanel leftpanel=new JPanel(new BorderLayout());//左面板 leftpanel.setBorder(new TitledBorder("Person")); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,rightpanel,leftpanel);//水平分隔窗格,左右各加入一个面板 split.setDividerLocation(140);//设置水平分隔条的位置 split.setEnabled(false);//设置分隔条不能变动 this.getContentPane().add(split);//框架内容窗格加入分隔窗格 text_user = new JTextArea(); text_user.setEditable(false); leftpanel.add(text_user); leftpanel.add(new JScrollPane(text_user));//设置文本编辑域能够滚动 text_number = new JTextField("1"); //编号文本行 text_number.setEditable(false); //不可编辑,编号自己主动生成 rightpanel.add(text_number); text_name = new JTextField("姓名"); rightpanel.add(text_name); JPanel panel_rb=new JPanel(new GridLayout(1,2)); //单选button子面板,网格布局。1行2列 rightpanel.add(panel_rb); ButtonGroup bgroup = new ButtonGroup(); //button组 radiob_male = new JRadioButton("男",true); //创建单选button。默认选中 bgroup.add(radiob_male); //单选button加入到button组 panel_rb.add(radiob_male); //单选button加入到子面板 radiob_female = new JRadioButton("女"); bgroup.add(radiob_female); panel_rb.add(radiob_female); this.cities = cities; combox_province = new JComboBox(provinces); //省份组合框 combox_province.setEditable(false); //设置组合框可编辑 combox_province.addActionListener(this); rightpanel.add(combox_province); combox_city = new JComboBox(cities[0]); //城市组合框 rightpanel.add(combox_city); button_add = new JButton("加入"); button_add.addActionListener(this); rightpanel.add(button_add); this.setVisible(true); } public void actionPerformed(ActionEvent e) //单击事件处理方法 { if (e.getSource()==combox_province) //在组合框的下拉列表中选择数据项时 { int i=combox_province.getSelectedIndex(); //省份组合框当前选中项序号 combox_city.removeAllItems(); //清除地区组合框中原全部内容 for (int j=0; j<this.cities[i].length; j++) combox_city.addItem(this.cities[i][j]); //地区组合框加入数据项 } if (e.getSource() == button_add) //单击button { String aline=number+", "+text_name.getText(); if (radiob_male.isSelected()) //指定单选button选中时 aline += ", "+radiob_male.getText(); //获得单选button表示的性别字符串 if (radiob_female.isSelected()) aline += ", "+radiob_female.getText(); aline += ", "+combox_province.getSelectedItem(); //获得组合框选中项的字符串 aline += ", "+combox_city.getSelectedItem(); text_user.append(aline+"\n"); //文本区加入一行字符串 this.number++; //编号自己主动加1 text_number.setText(""+this.number); text_name.setText("姓名"); } } public static void main(String arg[]) { Object provinces[]={"江苏省", "浙江省"}; Object cities[][]={{"南京市","苏州市","无锡市"}, {"杭州市","宁波市","温州市"}}; new UserJFrame(provinces, cities); } }