java之 ------ 圖形界面(三)
阿新 • • 發佈:2017-07-31
cati == item combo 輸入 tab grid sta line
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); } }
java之 ------ 圖形界面(三)