main.java
来自「电子与信息工程学生信息管理系统」· Java 代码 · 共 1,558 行 · 第 1/5 页
JAVA
1,558 行
if(obj==button_last)label.setText("the table last page!");
if(obj==button_prev)label.setText("the previous page!");
if(obj==button_next)label.setText("the next page!");
if(obj==button_sortId)label.setText("you have sorted the student with id!");
if(obj==button_sortName)label.setText("you have sorted the student with name!");
if(obj==button_sortSex)label.setText("you have sorted the student with sex!");
if(obj==button_sortAge)label.setText("you have sorted the student with age!");
if(obj==button_sortGrade)label.setText("you have sorted the student with grade!");
if(obj==button_sortGclass)label.setText("you have sorted the student with class!");
}
public void focusLost(FocusEvent e){
}
}
class TextName extends KeyAdapter{
public void keyTyped(KeyEvent e){
char ch = e.getKeyChar();
if(((ch<'Z'||ch=='Z')&&(ch>'A'||ch=='A'))||((ch<'z'||ch=='z'))&&((ch>'a'||ch=='a')||(ch==' ')));
else e.consume();
}
}
class TextId extends KeyAdapter{
public void keyTyped(KeyEvent e){
char ch = e.getKeyChar();
if(ch<'0'||ch>'9')e.consume();
}
}
}
class ButtonAction extends MyComponent{
Student one = new Student();
Student two = new Student();
Student three = new Student();
Student four = new Student();
Student first = new Student();
Student prev = new Student();
Student next = new Student();
RandomAccessFile raf;
ButtonEvent btn;
PopupMenuEvent menu;
public ButtonAction(){
one.next=two;
one.prev=null;
two.next=null;
two.prev=one;
//***********************************************read student node from file
try{
raf = new RandomAccessFile("2.txt","rw");
do{
listlenth++;
two.id = raf.readLine();
two.name = raf.readLine();
two.sex = raf.readLine();
two.age = raf.readLine();
two.grade = raf.readLine();
two.gclass = raf.readLine();
one.next=new Student();//build new student node begin
two.prev=one.next;
one.next.next=two;
one.next.prev=one;
two=one.next;//build new student node end
}while(!two.next.id.equals("!!!"));
listlenth--;
raf.close();
two.next.id="";
two.next.name="";
two.next.sex="";
two.next.age="";
two.next.grade="";
two.next.gclass="";
one = two;
two = two.next;
one.prev=null;
first=two.next;
for(int i=0;i<listlenth-1;i++){first=first.next;}
}catch(FileNotFoundException filea){return;}catch(IOException dfs){return;}
pageCombo=new String[listlenth/20+2];
pageCombo[0] ="--" ;
for(int i=1;i<listlenth/20+2;i++){
pageCombo[i]=String.valueOf(i);
}
comboBox_page=new JComboBox(pageCombo);
paneCenter3.add(comboBox_page);
paneCenter3.add(label_page);
//*******************************************read student node from file
//*******************************************display the student in table
three = two.next;
page=(listlenth/20)+1;
label_page.setText(String.valueOf(page)+"/"+String.valueOf(listlenth/20+1));
k=listlenth%20;
for(int i=0;i<k-1;i++){three=three.next;}
for(int p=0;p<k;three=three.prev){
data[p][0] = three.id;
data[p][0] = three.id;
data[p][1] = three.name;
data[p][2] = three.sex;
data[p][3] = three.age;
data[p][4] = three.grade;
data[p][5] = three.gclass;
p++;
}
table.repaint();
i=k;
k=0;
//*******************************************display the student in table
table.addMouseListener(new java.awt.event.MouseAdapter() {//table add mouselistener
public void mousePressed(MouseEvent e){
table_mousePressed(e);
}
});
table.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(MouseEvent e){
table_mouseReleased(e);
}
});
menu = new PopupMenuEvent();
menuEdit.addActionListener(menu);
menuDelete.addActionListener(menu);//table add mouselistener
btn = new ButtonEvent();//button add ActionListener
comboBox_page.addActionListener(btn);
button_input.addActionListener(btn);
button_search.addActionListener(btn);
//button_searchId.addActionListener(btn);
//button_searchName.addActionListener(btn);
button_sortId.addActionListener(btn);
button_sortName.addActionListener(btn);
button_sortSex.addActionListener(btn);
button_sortAge.addActionListener(btn);
button_sortGrade.addActionListener(btn);
button_sortGclass.addActionListener(btn);
button_next.addActionListener(btn);
button_prev.addActionListener(btn);
button_first.addActionListener(btn);
button_last.addActionListener(btn);//button add ActionListener
try{raf = new RandomAccessFile("E:\\2.sel","rw");
}catch(FileNotFoundException filea){return;}
}
void shuaxin(){for(int j=0;j<20;j++){data[j][0] = "";data[j][1] = "";data[j][2] = "";data[j][3] = "";data[j][4] = "";data[j][5] = "";}table.repaint();}
void displayTable(int page){//Student first,page
three=first;
label_page.setText(String.valueOf(page)+"/"+String.valueOf(listlenth/20+1));
for(int j=(page-1)*20;j>0&&three!=null;j--){three=three.prev;}
for(int i=0;i<20&&three!=null;i++){
data[i][0] = three.id;
data[i][1] = three.name;
data[i][2] = three.sex;
data[i][3] = three.age;
data[i][4] = three.grade;
data[i][5] = three.gclass;
three=three.prev;
}table.repaint();
}
void table_mousePressed(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e)){
Point point = e.getPoint();
row = table.rowAtPoint(point);
column = table.rowAtPoint(point);
table.changeSelection(row,column,false,false);
label.setText("you have selected the row "+row);
}
}
void table_mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
showPopupMenu(e.getX(),e.getY());
}
}
void showPopupMenu(int x,int y){
tablePopupMenu.show(table,x,y);
}
void writeFile( ){//Student two
four=two;
try{raf = new RandomAccessFile("2.txt","rw");}catch(FileNotFoundException filea){return;}
try{
for(int i=0;i<listlenth;i++){
raf.writeBytes(four.next.id+"\r\n");
raf.writeBytes(four.next.name+"\r\n");
raf.writeBytes(four.next.sex+"\r\n");
raf.writeBytes(four.next.age+"\r\n");
raf.writeBytes(four.next.grade+"\r\n");
raf.writeBytes(four.next.gclass+"\r\n");
four=four.next;
}
raf.writeBytes("!!!\r\n");
raf.close();
}catch(IOException eds){}
}
void comboBoxPage(){
paneCenter3.remove(comboBox_page);
pageCombo=new String[listlenth/20+2];
pageCombo[0] ="--" ;
for(int i=1;i<listlenth/20+2;i++){
pageCombo[i]=String.valueOf(i);
}
comboBox_page=new JComboBox(pageCombo);
paneCenter3.add(comboBox_page);
paneCenter3.add(label_page);
comboBox_page.addActionListener(btn);
}
void delete(){
if(listlenth==1){
two.next=null;
first=null;
listlenth--;i--;
writeFile();
shuaxin();
return;
}
if(page==1&&row==0){
first=first.prev;
first.next=null;
listlenth--;i--;
writeFile();
shuaxin();
displayTable(1);
return;
}
if(listlenth==(1+row+(page-1)*20)){two.next=two.next.next;two.next.prev=two;listlenth--;i--;writeFile();shuaxin();displayTable(page);return;}
three=first;
for(int k=0;k<(page-1)*20+row;k++){three=three.prev; }
four=three.next;
three.prev.next=four;
four.prev=three.prev;
i--;listlenth--;
writeFile();
shuaxin();
displayTable(page);
}
class ButtonEvent implements ActionListener{
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
//*******************************************************************buton_input action
shuaxin();
if(comboBox_page.getSelectedIndex()!=0)page=comboBox_page.getSelectedIndex();
displayTable(page);
if(obj==button_input){
two.id=textId.getText();
if (two.id.length()==0||two.id.length()>9){
Object[] options = {"yes"};
String str = "id lenth wrong";
JOptionPane.showOptionDialog(null,str,"warning",JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,null,options,options[0]);
return;
}
for(three=two;three.next!=null;three=three.next){
if( two.id.equals(three.next.id)){
Object[] options = {"yes"};
String str = "the id exist!";
JOptionPane.showOptionDialog(null,str,"warning",JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,null,options,options[0]);
two.id=null;
return;
}
}
two.name=textName.getText();
if (two.name.length()==0){
Object[] options = {"yes"};
String str = "name lenth wrong!";
JOptionPane.showOptionDialog(null,str,"warning",JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,null,options,options[0]);
return;
}
two.age=ageCombo[comboBox_age.getSelectedIndex()];
two.sex = sexCombo[comboBox_sex.getSelectedIndex()];
two.grade = gradeCombo[comboBox_grade.getSelectedIndex()];
two.gclass = classCombo[comboBox_class.getSelectedIndex()];
one.next=new Student();//build new student node begin
two.prev=one.next;
one.next.next=two;
one.next.prev=one;
two=one.next;//build new student node end
listlenth++;
if(listlenth==1){first=two.next;first.next=null;}
writeFile();
shuaxin();
page=listlenth/20+1;
displayTable(page);
textId.setText("");
textName.setText("");
comboBoxPage();
}
//*****************************************************************button_input action
//*****************************************************************button_prev action
if(obj==button_prev){
if(page==1) return;
page--;
shuaxin();
displayTable(page);
comboBox_page.setSelectedIndex(page);
}
//****************************************************************************button_prev action
//****************************************************************************button_next action
if(obj==button_next){
next = two.next;
if(page==(listlenth/20+1)) return;
page++;
shuaxin();
displayTable(page);
comboBox_page.setSelectedIndex(page);
}
//******************************************************************************button_next action
//******************************************************************************button_sortId action
if(obj==button_sortId){
click++;
click=click%2;
if(listlenth==1||listlenth==0)return;
if(listlenth==2){
if(Integer.parseInt(two.next.id)<Integer.parseInt(first.id)){
first.next=two.next;
first.prev=two;
two.next.next=null;
two.next.prev=first;
two.next=first;
first=first.next;
shuaxin();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?