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; Number Private JTextField text_number, Text_name; Number, Name text line private Jradiobutton Radiob_male, Radiob_female; Sex button private Object cities[][]; Store multi-provincial city private JComboBox combox_province, combox_city; Province, City combo box private JButton button_add; Add button private JTextArea text_user; Text Area public Userjframe (object provinces[], Object cities[][])//parameter specify province and city array {super ("Enter user Information"); This.setsize (740, 300); This.setlocationrelativeto (NULL); This.setdefaultcloseoperation (Exit_on_close); JPanel rightpanel=new JPanel (New GridLayout (6,1));//Right panel JPanel leftpanel=new JPanel (New BorderLayout ());//left panel LeftpanEl.setborder (New Titledborder ("person")); JSplitPane split=new JSplitPane (Jsplitpane.horizontal_split,rightpanel,leftpanel);///Horizontal divider pane, add a panel to the left and right Split.setdividerlocation (140);//Set the position of the horizontal divider split.setenabled (FALSE);//Set separator bar cannot be changed This.getcontentpane (). Add (SPL it);//The Frame Contents pane Adds a separator pane text_user = new JTextArea (); Text_user.seteditable (FALSE); Leftpanel.add (Text_user); Leftpanel.add (New JScrollPane (Text_user));//Set text edit field to scroll text_number = new JTextField ("1"); Numbered text line text_number.seteditable (false); Non-editable, numbered auto-generated rightpanel.add (Text_number); Text_name = new JTextField ("name"); Rightpanel.add (Text_name); JPanel panel_rb=new JPanel (new GridLayout); Radio button Sub-panel, grid layout, 1 rows 2 columns Rightpanel.add (PANEL_RB); Buttongroup bgroup = new Buttongroup (); Button Group Radiob_male = new Jradiobutton ("male", true); Create radio button, default check Bgroup.add (Radiob_male); The radio button is added to the button group Panel_rb.add (Radiob_male); Radio Button added to sub-panel Radiob_female = new Jradiobutton ("female"); Bgroup.add (Radiob_female); Panel_rb.add (Radiob_female); this.cities = cities; Combox_province = new JComboBox (provinces); Province combo box combox_province.seteditable (false); Set the combo box to edit combox_province.addactionlistener (this); Rightpanel.add (combox_province); combox_city = new JComboBox (cities[0]); City combo Box Rightpanel.add (combox_city); Button_add = new JButton ("Add"); Button_add.addactionlistener (this); Rightpanel.add (Button_add); This.setvisible (TRUE); public void actionperformed (ActionEvent e)//Click event handling method {if (E.getsource () ==combox_province) When you select a data item in the drop-down list of the combo box {int i=combox_province.getselectedindex (); The Province combo box is currently selected with the ordinal combox_city.RemoveAllItems (); Clears all contents of the Regional combo box for (int j=0; j<this.cities[i].length; j + +) Combox_city.additem (this.cities[i ][J]); Region combo box Add Data item} if (E.getsource () = = Button_add)//Click the button {String Alin E=number+ "," +text_name.gettext (); if (radiob_male.isselected ())//Specify the radio button aline + = "," +radiob_male.gettext (); Gets the gender string that the radio button represents if (Radiob_female.isselected ()) Aline + = "," +radiob_female.gettext (); Aline + = "," +combox_province.getselecteditem (); Gets the combo box of the selected item string aline + = "," +combox_city.getselecteditem (); Text_user.append (aline+ "\ n"); The text area adds a line of string this.number++; The number is automatically added 1 text_number.settext ("" +this.number); Text_name.settext ("name"); }} public static void Main (String arg[]) {Object PRovinces[]={"Jiangsu Province", "Zhejiang province"}; Object cities[][]={{"Nanjing", "Suzhou", "Wuxi"}, {"Hangzhou City", "Ningbo", "Wenzhou"}}; New Userjframe (provinces, cities); }}
Java------graphical Interface (iii)