java大作业-学生管理系统(1)

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class StudentFile extends JFrame implements ActionListener, ListSelectionListener {
    JList<Student> jList;
    DefaultListModel<Student> listModel;
    StudentJPanel studentJPanel;
    Font font;
    public StudentFile(Student[] students){
        this(students,new StudentJPanel());
    }
    public StudentFile(Student[] students,StudentJPanel studentJPanel){
        super("学生信息记录");
        this.setSize(900,700);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        font = new Font("楷体", Font.BOLD, 28);
        this.setFont(font);
        //分割窗格  让他分割成上边为输入信息面板,详见StudentJPanel类,下边面板布局为列表框和命令按钮
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        this.getContentPane().add(splitPane);
        this.studentJPanel = studentJPanel;
        splitPane.add(this.studentJPanel);
        splitPane.setDividerLocation(200);//上面占200
        splitPane.setOneTouchExpandable(true);//一键展开
        JPanel downPanel = new JPanel(new BorderLayout());//边布局
        splitPane.add(downPanel);
        this.listModel = new DefaultListModel<Student>();
        if (students != null){
            for (Student student : students) {
                this.listModel.addElement(student);
            }
            }
        this.jList = new JList<Student>(this.listModel);
        this.jList.addListSelectionListener(this);
        this.jList.setFixedCellHeight(30);
        this.jList.setFont(font);
        downPanel.add(new JScrollPane(this.jList));
        JPanel cmdPanel = new JPanel();//面板默认流布局
        downPanel.add(cmdPanel,"South");
        String[]str = {"添加","修改","删除"};
        for (int i = 0; i < str.length; i++) {
            JButton button = new JButton(str[i]);
            button.addActionListener(this);
            cmdPanel.add(button);
        }

        this.setVisible(true);
    }
    public StudentFile(){
        this(null,new StudentJPanel());
    }

    public void reset(){
        this.studentJPanel.text_id.setText(" 学号 : ");
        this.studentJPanel.text_name.setText(" 姓名 : ");
        this.studentJPanel.text_classId.setText(" 班级 : ");
        this.studentJPanel.text_school.setText(" 学校 : ");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()instanceof JButton){
            Student stu = null;
            switch (e.getActionCommand()){
                case "添加":
                    if ((stu = this.studentJPanel.get())!=null)
                    {
                        this.listModel.addElement(stu);
                        reset();
                    }
                    break;
                case "修改":
                    this.change(this.jList,this.listModel,null);
                    reset();
                    break;
                case "删除":
                    this.remove(this.jList,this.listModel);
                    reset();
                    break;
            }
        }
    }

    public <T> void remove(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,"确认删除"+str+"?","删除",JOptionPane.YES_NO_OPTION)==0)
                    this.listModel.removeElementAt(i);
            }
        }
    }

    public <T> void change(JList jList, DefaultListModel<T> listModel,Student stu){
        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,"确认修改"+str+"?","修改",JOptionPane.YES_NO_OPTION)==0){
                    this.listModel.removeElementAt(i);
                    if ((stu = this.studentJPanel.get())!=null)
                        this.listModel.addElement(stu);
                }
            }
        }

    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        this.studentJPanel.set(this.jList.getSelectedValue());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值