日期:2014-05-20 浏览次数:20881 次
DefaultListModel model = new DefaultListModel(); JList list = new JList(model);
------解决方案--------------------
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSplitPane; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class MainFrame extends JFrame implements ActionListener,ListSelectionListener { private JTextField name; private JTextField age; private JTextField tel; private JRadioButton man; private JRadioButton women; private JButton add; private JButton del; private JList list; private Vector<Student> students; public MainFrame() { //创建输入信息的部分开始 name = new JTextField(20); age = new JTextField(20); tel = new JTextField(20); man = new JRadioButton("男"); man.setSelected(true); women = new JRadioButton("女"); JPanel input = new JPanel(); input.setLayout(new FlowLayout()); input.setSize(50, 150); JPanel p1 = new JPanel(); p1.add(new JLabel("姓名:")); p1.add(name); input.add(p1); JPanel p2 = new JPanel(); p2.add(new JLabel("年龄:")); p2.add(age); input.add(p2); JPanel p3 = new JPanel(); p3.add(new JLabel("电话:")); p3.add(tel); input.add(p3); JPanel p4 = new JPanel(); p4.add(new JLabel("性别:")); p4.add(man); p4.add(women); input.add(p4); //加入到ButtonGroup中。这样的话同时只能选中一个。 ButtonGroup bg = new ButtonGroup(); bg.add(man); bg.add(women); //创建输入信息的部分结束 //创建按钮部分开始 add = new JButton("增加"); add.addActionListener(this); del = new JButton("删除"); del.addActionListener(this); JPanel buttons = new JPanel(); buttons.add(add); buttons.add(del); this.add(buttons,BorderLayout.NORTH); //创建按钮部分结束 //创建JList部分开始 students = new Vector<Student>(); Student stu = new Student(); stu.setName("liuming"); //students.add(stu); list = new JList(students); list.addListSelectionListener(this); //创建JList部分开始 JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); jsp.setDividerLocation(350); jsp.setDividerSize(3); jsp.setLeftComponent(input); jsp.setRightComponent(list); this.add(jsp); this.setTitle("简单学生信息管理"); this.setSize(500,400); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //处理按钮事件 public void actionPerformed(ActionEvent e) { if(e.getSource() == add) { if(check()) return; Student student = new Student(); student.setName(name.getText()); student.setAge(Integer.parseInt(age.getText())); if(man.isSelected()) { student.setSex("男"); } else if(women.isSelected()) { student.setSex("女"); } student.setTel(tel.getText()); students.add(student); list.setListData(students); list.repaint(); JOptionPane.showMessageDialog(this, "添加学生信息成功!"); } else if(e.getSource() == del) { int index = list.getSelectedIndex(); if(index >= 0) { Student stu = students.remove(index); list.setListData(students); list.repaint(); JOptionPane.showMessageDialog(this, "删除学生" + stu.getName() + "的信息成功!"); } else { JOptionPane.showMessageDialog(this, "没有选中学生!"); } } } //判断是否输入有空值,年龄是否是数字 private boolean check() { boolean flag = false; if(name.getText().equals("")) { flag = true; JOptionPane.showMessageDialog(this, "姓名不能为空!"); } else if(age.getText().equals("")) { flag = true; JOptionPane.showMessageDialog(this, "年龄不能为空!"); } else if(tel.getText().equals("")) { flag = true; JOptionPane.showMessageDialog(this, "电话不能为空!"); } else if(!age.getText().matches("(\\d)++")) { flag = true; JOptionPane.showMessageDialog(this, "年龄必须为的数字!"); } return flag; } //处理JList的值改变的事件 public void valueChanged(ListSelectionEvent e) { int index = list.getSelectedIndex(); Student stu = students.get(index); String info = "姓名:" + stu.getName() + "\n年龄:" + stu.getAge() + "\n性别:" + stu.getSex() + "\n电话:" + stu.getTel(); JOptionPane.showMessageDialog(this, info); } public static void main(String[] args) { new MainFrame(); } }