package work;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class StudentJFrame extends JFrame implements ActionListener, ListSelectionListener {
protected JList<Student> jlist;
protected DefaultListModel<Student> listmodel;
protected StudentJPanel student;
protected JComboBox<String>[] comboxs;
private static Equalable[] equal = { new EqualName(), new EqualBirthdate() };
private static Comparator[] comparators = { new CompareName(), new CompareBirthdate() };
public StudentJFrame(Student[] stu) {
this(stu, new StudentJPanel());
}
public StudentJFrame(Student[] stu, StudentJPanel student) {
super("Student 对象信息管理");
this.setSize(700, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
this.getContentPane().add(split);
this.student = student;
split.add(this.student);
split.setDividerLocation(110);
split.setOneTouchExpandable(true);
JPanel rightpanel = new JPanel(new BorderLayout());
split.add(rightpanel);
this.listmodel = new DefaultListModel<Student>();
if (stu != null)
for (int j = 0; j < stu.length; j++)
this.listmodel.addElement(stu[j]);
this.jlist = new JList<Student>(this.listmodel);
this.jlist.addListSelectionListener(this);
rightpanel.add(new JScrollPane(this.jlist));
JPanel cmdpanel = new JPanel();
rightpanel.add(cmdpanel, "South");
String[][] str = { { "添加", "删除", "删除选中项" }, { "查找关键字", "排序关键字" }, { "姓名", "出生日期" } };
for (int i = 0; i < str[0].length; i++) {
JButton bt = new JButton(str[0][i]);
bt.addActionListener(this);
cmdpanel.add(bt);
}
this.comboxs = new JComboBox[str[1].length];
for (int i = 0; i < str[1].length; i++) {
cmdpanel.add(new JLabel(str[1][i]));
cmdpanel.add(this.comboxs[i] = new JComboBox<String>(str[2]));
this.comboxs[i].addActionListener(this);
}
this.setVisible(true);
}
public StudentJFrame() {
this(null, new StudentJPanel());
}
public void valueChanged(ListSelectionEvent event) {
this.student.set(this.jlist.getSelectedValue());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() instanceof JButton)
{
Student stu1=null;
switch (e.getActionCommand())
{
case "添加":
//单击“添加”按钮
if((stu1=this.student.get())!=null) //per获得Person实例,运行时多态
this.listmodel.addElement(stu1); //列表框模型添加Person对象
break;
case "删除": //单击“删除”按钮
remove(this.listmodel, this.student.get());
break;
case "删除选中项":
this.removeSelected(this.jlist, this.listmodel);
break;
case "删除选中多项": //第5版第6章没写,第4版例12.2
this.removeSelectedAll(this.jlist, this.listmodel);
break;
}
}
else if(e.getSource() instanceof JComboBox) //查找和排序组合框
{
// jlist.clearSelection(); //清除所有选中状态
// if(e.getSource()==this.comboxs[0]) //查找组合框
// {
// int i=this.comboxs[0].getSelectedIndex(); //获得查找关键字序号
// if(i<equal.length) //为什么?过滤子类增加的数据项序号
// selectAll(this.listmodel, this.jlist, this.student.get(), equal[i]);
//查找并选中所有与Student相等对象
// ListModelOper.selectAll(this.listmodel, this.jlist, this.Student.get(), equal[i]);
// return;
// }
if(e.getSource()==this.comboxs[1]) //排序组合框
{
int i=this.comboxs[1].getSelectedIndex(); //获得排序关键字序号
if(i<comparators.length) //为什么?过滤子类增加的数据项序号
sort(this.listmodel, comparators[i]); //列表框模型数据项排序
// ListModelOper.sort(this.listmodel, comparators[i]); //列表框模型数据项排序
}
}
}
//在listmodel列表框模型中删除与obj相等的数据项,通用方法
public <T> void remove(DefaultListModel<T> listmodel, T obj)
{
//下句弹出确认对话框见图6-14(b),单击“是”按钮返回0,单击“否”按钮返回1
if(obj!=null && JOptionPane.showConfirmDialog(this, "删除 "+obj.toString()+"?", "确认",
JOptionPane.YES_NO_OPTION)==0)
{
//下句在listmodel列表框模型中删除首个与obj指定对象相等的数据项,
//顺序查找算法,默认调用obj的equals(Object)方法比较对象相等。
boolean remove = this.listmodel.removeElement(obj);
JOptionPane.showMessageDialog(this, remove?"删除成功":"查找不成功,没有删除");
}
}
//将jlist列表框选中项行数据在listmodel列表框模型中删除,通用方法
public <T> void removeSelected(JList jlist, DefaultListModel<T> listmodel)
{
if(this.listmodel.getSize()==0) //返回列表框数据项数
JOptionPane.showMessageDialog(this, "列表框为空,不能删除");
else
{
int i=this.jlist.getSelectedIndex(); //列表框选中数据项序号
if(i==-1)
JOptionPane.showMessageDialog(this, "请选中列表框数据项");
else
{
String str=this.jlist.getSelectedValue().toString(); //列表框选中数据项字符串
if(JOptionPane.showConfirmDialog(this, "删除 第"+i+"项("+str+")?","确认",
JOptionPane.YES_NO_OPTION)==0) //确认对话框,单击“是”按钮,则返回0
this.listmodel.removeElementAt(i); //删除列表框第i数据项
}
}
}
//说明。(1)如果列表框空,或没有选中一项,i==-1,jlist.getSelectedValue()返回null。
//(2)removeElementAt(-1),抛出异常ArrayIndexOutOfBoundsException: Array index out of range: -1
//第12章,§12.3.2 列表框 1.列表框多项选择
//【课程设计12】列表框删除选中多项。使用ArrayList<T>
//将jlist列表框选中多项数据在listmodel列表框模型中全部删除,通用方法。
public <T> void removeSelectedAll(JList jlist, DefaultListModel<T> listmodel)
{
try
{ //下句当列表框选中多项时,返回列表集合对象
ArrayList<Student> list=(ArrayList<Student>)this.jlist.getSelectedValuesList();
for(Student per:list) //逐元循环,per获得list集合中的每个元素
this.listmodel.removeElement(per); //删除列表框对象值为per的数据项
}
catch(ClassCastException ex) //如果列表框空,或没有选中一项,则抛出异常
{
JOptionPane.showMessageDialog(this, "列表框空,或没有选中数据项,不能删除");
}
}
//说明。(1)jlist.getSelectedValues();方
没有合适的资源?快使用搜索试试~ 我知道了~
《JTable编写GUI学生对象管理系统》源码
共28个文件
class:15个
java:5个
xml:3个
0 下载量 125 浏览量
2023-06-29
12:04:26
上传
评论
收藏 23KB RAR 举报
温馨提示
资源标题:《JTable编写GUI学生对象管理系统》 资源描述: 本资源提供了一个使用JTable编写的GUI学生对象管理系统的示例。通过该示例,您可以学习如何使用Java Swing库中的JTable组件来展示和管理学生对象的数据。适用于Java开发者和学习者,尤其是对GUI编程和数据管理有兴趣的人群。 使用场景及目标: 该资源适用于希望学习如何使用JTable组件来创建GUI应用程序的人群。您可以通过该示例了解如何创建一个简单的学生对象管理系统,包括添加、删除、编辑和展示学生信息等功能。通过实践,您可以加深对JTable的理解,并掌握在GUI应用程序中展示和管理数据的技巧。 其他说明: 在学习过程中,您需要具备Java编程的基础知识,并熟悉Swing库的基本概念和使用方法。本示例提供了完整的源代码和详细的注释,以便您更好地理解和运行示例程序。 关键词:JTable、GUI、学生对象管理、Swing、Java编程、数据展示、数据管理
资源推荐
资源详情
资源评论
收起资源包目录
JTableDemo.rar (28个子文件)
JTableDemo
.classpath 396B
.settings
org.eclipse.jdt.core.prefs 781B
org.eclipse.core.resources.prefs 57B
src
work
StudentJFrame.java 12KB
test.java 5KB
JTableExample.java 1008B
Student.java 46B
StudentManagementSystem.java 7KB
.idea
workspace.xml 2KB
misc.xml 267B
modules.xml 267B
JTableDemo.iml 477B
bin
work
StudentManagementSystem$2.class 749B
StudentJFrame$EqualBirthdate.class 2KB
test.class 1KB
StudentJFrame$Equalable.class 2KB
StudentJFrame$EqualName.class 2KB
Student.class 256B
JTableExample.class 1KB
StudentJFrame$CompareName.class 2KB
StudentManagementSystem$3.class 757B
StudentJFrame.class 4KB
StudentManagementSystem$1.class 746B
StudentManagementSystem$5.class 1KB
StudentManagementSystem$4.class 747B
StudentManagementSystem.class 6KB
StudentJFrame$CompareBirthdate.class 2KB
.project 386B
共 28 条
- 1
资源评论
wusp1994
- 粉丝: 2531
- 资源: 881
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功