📄 servermainframe.java
字号:
//表格初始化
public void initTable(){
m=new DefaultTableModel(data,name);
table=new JTable(m);
table.setBackground(new Color(240,248,255));
table.setForeground(new Color(67,26,5));
//设置各列宽度
TableColumn column;
column=table.getColumnModel().getColumn(0);
column.setPreferredWidth(100);
column=table.getColumnModel().getColumn(1);
column.setPreferredWidth(45);
column=table.getColumnModel().getColumn(2);
column.setPreferredWidth(15);
column=table.getColumnModel().getColumn(3);
column.setPreferredWidth(80);
column=table.getColumnModel().getColumn(4);
column.setPreferredWidth(45);
column=table.getColumnModel().getColumn(5);
column.setPreferredWidth(85);
column=table.getColumnModel().getColumn(6);
column.setPreferredWidth(14);
column=table.getColumnModel().getColumn(7);
column.setPreferredWidth(150);
table.setFont(new Font("宋体",Font.PLAIN,12));
table.setGridColor(new Color(31,86,216));
table.setSelectionBackground(new Color(239,227,253));
table.setSelectionForeground(new Color(253,29,11));
}
//菜单初始化
public void initMenu(){
fileMenu.add(addMenuItem);
addMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addEvent();
}
});
fileMenu.add(seeMenuItem);
seeMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
vector=FileEvent.getStudent(
String.valueOf(list.getSelectedValue())); //获得所有学生数据信息
setData(vector);
}
});
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
exitMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
bianjiMenu.add(delMenuItem);
delMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
deleteEvent();
}
});
bianjiMenu.add(resetMenuItem);
resetMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
resetEvent();
}
});
editMenu.add(arrayItem1);
arrayItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
arrayByStudentId();
}
});
editMenu.add(arrayItem2);
arrayItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
arrayByStudentName();
}
});
editMenu.add(arrayItem3);
arrayItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
arrayByStudentClass();
}
});
editMenu.addSeparator();
searchMenu.add(searchItem1); //查找子菜单
searchItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
searchById();
}
});
searchMenu.add(searchItem2);
searchItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
searchByName(); //按姓名查找
}
});
editMenu.add(searchMenu);
bar.add(fileMenu);
bar.add(bianjiMenu);
bar.add(editMenu);
}
//增加新的学生信息
public void addEvent(){
StudentAddFrame s=new StudentAddFrame();
if(!(list.isSelectionEmpty()))
s.text[3].setText(list.getSelectedValue().toString());
s.show();
s.pack();
}
//设置数据项(同时更新表格)
public static void setData(Vector v){
data=new Object[v.size()][name.length];
for(int i=0;i<v.size();i++){
data[i][0]=((Student)v.elementAt(i)).getStudentId();
data[i][1]=((Student)v.elementAt(i)).getStudentName();
data[i][2]=((Student)v.elementAt(i)).getStudentSex();
data[i][3]=((Student)v.elementAt(i)).getStudentMajor();
data[i][4]=((Student)v.elementAt(i)).getStudentClass();
data[i][5]=((Student)v.elementAt(i)).getStudentLesson();
data[i][6]=((Student)v.elementAt(i)).getStudentMark();
data[i][7]=((Student)v.elementAt(i)).getStudentPhone();
}
System.out.println("data size:"+data.length);
tableUpdate();
}
//删除表格中所有项
public static void clearTable(){
for(int i=0;i<m.getRowCount();i++) //移出当前表中所有元素
while(m.getRowCount()!=0)
m.removeRow(i);
}
//带参数的更新表格
public static void updateTable(Object[][] d){
data=d;
tableUpdate();
}
//更新表格
public static void tableUpdate(){
clearTable();
System.out.println("vector size:"+vector.size());
for(int j=0;j<data.length;j++)
m.addRow(data[j]);
}
//删除选中的表格项
public void deleteEvent(){
int row=table.getSelectedRow();
if(row==-1){
MsgDialog dialog=new MsgDialog(this,"请选择要删除的项!"); //如果为选中所要删除的项,则弹出出错对话框
}
else{
String del=((Student)(vector.elementAt(row))).getStudentName();
String maj=((Student)(vector.elementAt(row))).getStudentMajor();
vector.removeElementAt(row);
m.removeRow(row);
FileEvent.deleteStudent(maj,row);
FileEvent.saveHistoryInfor(del,1); //保存历史纪录
setData(FileEvent.getStudent(maj));
}
}
//修改选中的表格项
public void resetEvent(){
int row=table.getSelectedRow();
if(row==-1){
MsgDialog dialog=new MsgDialog(this,"请选择要修改的项!");
}
else{
String m=(((Student)(vector.elementAt(table.getSelectedRow()))).getStudentMajor());
System.out.println("要修改的专业名为:"+m);
ResetStudentFrame resetFrame=new ResetStudentFrame(vector,m,row);
for(int i=0;i<8;i++)
resetFrame.text[i].setText(data[row][i].toString());
resetFrame.setLocation(260,280);
}
}
//当增加新的学生信息后,更新列表中的项
public static void updateList(Vector listname){
listModel.removeAllElements();
for(int j=0;j<listname.size();j++)
listModel.addElement(listname.elementAt(j));
}
//删除列表中的项
public void deleteListItem(){
Object object;
String delitem=list.getSelectedValue().toString();
Vector majorvector=new Vector();
majorvector=FileEvent.getFileName();
int index=majorvector.indexOf((Object)delitem);
listDelBtn.setEnabled(false);
File file=new File("com/herb/server/Infor/student//"+delitem+".txt");
FileDeleteDialog deletedialog=new FileDeleteDialog(this,file);
if(FileDeleteDialog.deleteNow()){ //如果返回为真,则删除选项
listModel.remove(index);
majorvector.remove(index);
FileEvent.saveFileName(majorvector); //保存修改后的结果
}
else
listDelBtn.setEnabled(true);
deletedialog.setResizable(false);
}
//按字符串排序
public static void arrayByString(int n){
int row=data.length;
Object temp;
for(int i=0;i<row-1;i++){
for(int j=0;j<row-1;j++){
if((String.valueOf(data[j][n]).compareToIgnoreCase
(String.valueOf(data[j+1][n])))>0){
for(int z=0;z<8;z++){
temp=data[j][z];
data[j][z]=data[j+1][z];
data[j+1][z]=temp;
}
}
}
}
tableUpdate();
}
//排序后保存信息
public static void saveInforAfterArray(){
Vector v=new Vector();
for(int i=0;i<data.length;i++){
int j=0;
Vector vv=new Vector();
while(j<name.length)
vv.add(data[i][j++].toString());
System.out.println("@ "+vv.toString());
Student student=new Student((String)vv.elementAt(0),(String)vv.elementAt(1),
(String)vv.elementAt(2),(String)vv.elementAt(3),
(String)vv.elementAt(4),(String)vv.elementAt(5),
(String)vv.elementAt(6),(String)vv.elementAt(7));
v.add(student);
}
String m=((Student)v.elementAt(0)).getStudentMajor();
FileEvent.saveStudentInfor(m,v);
}
//按学号排序
public void arrayByStudentId(){
if(table.getRowCount()==0)
arrayWarn();
else{
int row=data.length;
Object temp;
for(int i=0;i<row-1;i++){
for(int j=0;j<row-1;j++){
String a=String.valueOf(data[j][0]);
String b=String.valueOf(data[j+1][0]);
int aa,bb;
if(a.equals(""))
a=new String("0"); //如果输入“”,则会产生异常
if(b.equals(""))
b=new String("0");
aa=Integer.parseInt(a);
bb=Integer.parseInt(b);
if(aa>bb){
for(int z=0;z<8;z++){
temp=data[j][z];
data[j][z]=data[j+1][z];
data[j+1][z]=temp;
}
}
}
}
tableUpdate();
saveInforAfterArray();
}
}
//按班级排序
public void arrayByStudentClass(){
if(table.getRowCount()==0)
arrayWarn();
else{
arrayByString(4);
saveInforAfterArray();
}
}
//按姓名排序
public void arrayByStudentName(){
if(table.getRowCount()==0)
arrayWarn();
else{
arrayByString(1);
saveInforAfterArray();
}
}
//按姓名查找
public void searchByName(){
new SearchFrame(this,2,"姓名");
setBtnEnabled(false);
}
//按学号查找
public void searchById(){
new SearchFrame(this,1,"学号");
setBtnEnabled(false);
}
//当选择查询时,因为技术原因,无法解决在查询状态下依然能做这些操作,为了
//避免出现错误,故将这些按钮设为不可用状态
public void setBtnEnabled(boolean n){
delBtn.setEnabled(n);
xiuBtn.setEnabled(n);
paixuBtn1.setEnabled(n); //设为false,否则会有不可预知的错误
paixuBtn2.setEnabled(n);
paixuBtn3.setEnabled(n);
dlBtn.setEnabled(n);
resetBtn.setEnabled(n);
array1.setEnabled(n);
array2.setEnabled(n);
array3.setEnabled(n);
arrayItem1.setEnabled(n);
arrayItem2.setEnabled(n);
arrayItem3.setEnabled(n);
}
public void arrayWarn(){
new MsgDialog(this,"请先双击系名列表中的项,再进行查找!");
}
public static void main(String args[]){
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
new ServerMainFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -