標籤:圖形介面 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; //性別按鈕 private Object cities[][]; //儲存多省的城市 private JComboBox combox_province, combox_city; //省份、城市組合框 private JButton button_add; //添加按鈕 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)); //選項按鈕子面板,網格布局,1行2列 rightpanel.add(panel_rb); ButtonGroup bgroup = new ButtonGroup(); //按鈕組 radiob_male = new JRadioButton("男",true); //建立選項按鈕,預設選中 bgroup.add(radiob_male); //選項按鈕添加到按鈕組 panel_rb.add(radiob_male); //選項按鈕添加到子面板 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) //單擊按鈕 { String aline=number+", "+text_name.getText(); if (radiob_male.isSelected()) //指定選項按鈕選中時 aline += ", "+radiob_male.getText(); //獲得選項按鈕表示的性別字串 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之 ------ 圖形介面(三)