JAva大宿題-学生管理システム(1)
32238 ワード
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());
}
}