PersonJFrame

来源:互联网 发布:linux重启eth0网卡命令 编辑:程序博客网 时间:2024/06/05 06:43

package cn.hncu.exersice6_4;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ButtonGroup;import javax.swing.JComboBox;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JTextField;import javax.swing.border.TitledBorder;public class PersonJPanel extends JPanel implements ActionListener {private static final long serialVersionUID = 1L;protected JTextField texts[];protected JRadioButton radiobutton_sex[];protected JComboBox<String> combobox_province, combobox_city;private static String[] province_str = { "江苏省", "浙江省", "湖南省" };private static String[][] city_str = { { "南京市", "苏州市", "无锡市" },{ "南京市", "苏州市", "无锡市" }, { "长沙", "娄底", "益阳" } };public PersonJPanel() {setBorder(new TitledBorder("Person"));setLayout(new GridLayout(5, 1));String[][] initialize_personjpanel = { { "姓名", "1970年1月1日" },{ "男", "女" } };texts = new JTextField[initialize_personjpanel[0].length];for (int i = 0; i < initialize_personjpanel[0].length; i++) {texts[i] = new JTextField(initialize_personjpanel[0][i]);add(texts[i]);}JPanel panel_radiobuttons = new JPanel();panel_radiobuttons.setLayout(new GridLayout(1, 2));add(panel_radiobuttons);ButtonGroup buttongroup = new ButtonGroup();radiobutton_sex = new JRadioButton[initialize_personjpanel[1].length];for (int i = 0; i < initialize_personjpanel[1].length; i++) {panel_radiobuttons.add(radiobutton_sex[i] = new JRadioButton(initialize_personjpanel[1][i]));buttongroup.add(radiobutton_sex[i]);}radiobutton_sex[0].setSelected(true);add(combobox_province = new JComboBox<String>(province_str));add(combobox_city = new JComboBox<String>(city_str[0]));combobox_province.addActionListener(this);combobox_city.addActionListener(this);combobox_city.setEditable(true);}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == combobox_province) {int i = combobox_province.getSelectedIndex();if (combobox_city != null && i != -1) {combobox_city.removeAllItems();for (int j = 0; j < city_str[i].length; j++) {combobox_city.addItem(city_str[i][j]);}}}if (e.getSource() == combobox_city) {String get_item = (String) combobox_city.getSelectedItem();if (get_item == null) {return;}insert(combobox_city, get_item);}}public void set(Person p) {if (p == null) {return;}this.texts[0].setText(p.name);texts[1].setText(p.birthday.toString());if (p.sex.equals("男")) {this.radiobutton_sex[0].setSelected(true);} else {this.radiobutton_sex[1].setSelected(true);}this.combobox_province.setSelectedItem(p.province);this.combobox_city.setSelectedItem(p.city);}public Person get() {String sex = radiobutton_sex[0].isSelected() ? radiobutton_sex[0].getText() : radiobutton_sex[1].getText();return new Person(texts[0].getText(), new MyDate().setMyDate(texts[1].getText()), sex, (String) combobox_province.getSelectedItem(),(String) combobox_city.getSelectedItem());}public <T extends Comparable<? super T>> void insert(JComboBox<T> combobox,T value) {// 书上抄的,希望你明白,**有一点强大,T 可以接受sting and int// 类型,但是这里被定义为stringint begin = 0, end = combobox.getItemCount() - 1, middle = end;while (begin <= end) {middle = (begin + end) / 2;if (value.compareTo(combobox.getItemAt(middle)) == 0) {return;}if (value.compareTo(combobox.getItemAt(middle)) < 0) {end = middle - 1;} else {begin = middle + 1;}}combobox.insertItemAt(value, begin);}}

package cn.hncu.exersice6_4;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Comparator;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;public class PersonJFrame extends JFrame implements ListSelectionListener,ActionListener {private static final long serialVersionUID = 1L;protected PersonJPanel leftpanel;protected JPanel rightpanel;protected JComboBox comboboxs[];protected JList<Person> jlist;protected DefaultListModel<Person> listmodel;private static Equalable equal[] = { new EqualName(), new EqualBirthday() };private static Comparator comparators[] = { new CompareName(),new CompareBirthday() };public PersonJFrame() {}public PersonJFrame(Person[] value) {super("Person 对象信息管理");setSize(1100, 300);setLocationRelativeTo(null);setDefaultCloseOperation(EXIT_ON_CLOSE);this.leftpanel = new PersonJPanel();rightpanel = new JPanel(new BorderLayout());JSplitPane splitpanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpanel, rightpanel);splitpanel.setDividerLocation(140);getContentPane().add(splitpanel);listmodel = new DefaultListModel<Person>();if (value != null) {for (int i = 0; i < value.length; i++) {listmodel.addElement(value[i]);}}jlist = new JList<Person>(listmodel);jlist.addListSelectionListener(this);rightpanel.add(new JScrollPane(jlist));JPanel buttonpanel = new JPanel();rightpanel.add(buttonpanel, "South");String[][] buttonsting_north = { { "添加", "删除", "删除选中项" },{ "查找关键字", "排序关键字" }, { "姓名", "出生日期" } };for (int i = 0; i < buttonsting_north[0].length; i++) {JButton btn = new JButton(buttonsting_north[0][i]);btn.addActionListener(this);buttonpanel.add(btn);}comboboxs = new JComboBox[buttonsting_north[1].length];for (int i = 0; i < buttonsting_north[1].length; i++) {comboboxs[i] = new JComboBox<String>(buttonsting_north[2]);buttonpanel.add(new JLabel(buttonsting_north[1][i]));buttonpanel.add(comboboxs[i]);comboboxs[i].addActionListener(this);}setVisible(true);}public static void main(String[] args) {Person[] value = {new Person("Li李小明", new MyDate(1992, 3, 15), "男", "湖南", "长沙"),new Person("Bai白杨", new MyDate(1991, 5, 1), "女", "湖北", "武汉"),new Person("Bai白桦", new MyDate(1992, 3, 15), "男", "广西", "南宁"),new Person("Hua华文", new MyDate(1992, 10, 3), "女", "广东", "广州"),new Person("Wang王伟", new MyDate(1990, 4, 3), "男", "广东", "广州"),new Student("张莉", new MyDate(1992, 4, 3), "女", "湖北", "武汉","信息科学与工程学院", "信息管理与信息技术专业", "031", true),new Student(),new Student(new Person("张三", new MyDate(1992, 3, 15), "男","广西", "南宁"), "信息科学与工程学院", "信息管理与信息技术专业", "031", true) };new PersonJFrame(value);}@Overridepublic void valueChanged(ListSelectionEvent e) {this.leftpanel.set(this.jlist.getSelectedValue());}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("删除选中项")) {if (listmodel.getSize() == 0) {JOptionPane.showMessageDialog(this, "列表以空!!");return;}int i = this.jlist.getSelectedIndex();if (i == -1) {JOptionPane.showConfirmDialog(this, "请选中列表框数据项");} else {this.listmodel.removeElementAt(i);}}if (e.getActionCommand().equals("删除")) {this.leftpanel.set(new Person());return;}if (e.getActionCommand().equals("添加")) {this.listmodel.addElement(this.leftpanel.get());}if (e.getSource() == comboboxs[0]) {int i = this.comboboxs[0].getSelectedIndex();if (i < equal.length) {search(this.leftpanel.get(), equal[i]);}}if (e.getSource() == comboboxs[1]) {int i = this.comboboxs[1].getSelectedIndex();if (i < comparators.length) {sort(comparators[i]);}}}public <T> void search(T obj, Equalable<? super T> e) {int n = this.listmodel.getSize();for (int i = 0; i < n; i++) {if (e.equals(obj, (T) this.listmodel.elementAt(i))) {this.jlist.setSelectedIndex(i);return;}}}public <T> void sort(Comparator<? super T> c) {for (int i = 0; i < listmodel.getSize() - 1; i++) {int min = i;for (int j = i + 1; j < listmodel.getSize(); j++) {if (c.compare((T) listmodel.getElementAt(j),(T) listmodel.getElementAt(min)) < 0) {min = j;}}if (min != i) {Person temp = this.listmodel.getElementAt(i);listmodel.setElementAt(listmodel.getElementAt(min), i);listmodel.setElementAt(temp, min);}}}}






0 0
原创粉丝点击