📄 score.java
字号:
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class Score extends JFrame
{
private InformationList list;
private JTable table;
private SingleTableModel model;
private JScrollPane scrollPane;
private ObjectInputStream in;
private ObjectOutputStream out;
private File file;
public Score(InformationList pList)
{
super("Score");
list=pList;
model=new SingleTableModel(list);
table=new JTable(model);
model.addTableModelListener(new TableModelListener(){
public void tableChanged(TableModelEvent e)
{
modelListener(e);
}
});
table.setPreferredScrollableViewportSize(new Dimension(350,250));
table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);
table.setRowHeight(30);
table.getTableHeader().setBackground(Color.PINK);
table.getTableHeader().setForeground(Color.blue);
table.setBackground(new Color(243,224,254));
table.setForeground(Color.blue);
table.setSelectionBackground(Color.orange);
scrollPane=new JScrollPane(table);
scrollPane.setBackground(Color.pink);
scrollPane.setForeground(Color.pink);
this.setLayout(new BorderLayout());
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(((int)screen.getWidth()-350)/2,((int)screen.getHeight()-250)/2);
this.setSize(350,250);
this.pack();
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
try{
out=new ObjectOutputStream(new FileOutputStream("score.friend"));
list.displayList();
// System.out.println("jianchashifouchongxin xiehui");
out.writeObject(list);
out.flush();
out.close();
if(list.getSize()!=0)
{
for(int i=0;i<list.getSize();i++)
{
//System.out.println("Row: "+i+"Name :"+((Information)list.search(i)).getUserName()+"Score: "+((Information)list.search(i)).getScore());
}
}
}catch(Exception ev)
{
System.out.println(ev);
}
dispose();
}
});
}
public void modelListener(TableModelEvent e)
{
if(e.getColumn()==1)
{
//System.out.println("进入里面");
//System.out.println((String)model.getValueAt(e.getFirstRow(),1));
list.search(e.getFirstRow()+1).setUserName((String)model.getValueAt(e.getFirstRow(),1));
//System.out.println(list.search(e.getFirstRow()+1).getUserName());
//System.out.println("Row: "+(e.getFirstRow())+((Information)list.get(e.getFirstRow())).getUserName());
}
}
}
class SingleTableModel extends AbstractTableModel
{
private String[] columnNames={
"排名","姓名","得分"
};
private InformationList list;
//private LinkedList list;
private Object[][] data;
public SingleTableModel(InformationList list)
{
this.list=list;
// Object [] element=list.toArray();
Information p=list.getFirst();
data=new Object[list.getSize()][3];
for(int i=0;i<list.getSize()&&p.getNext()!=null;i++)
{
p=p.getNext();
for(int j=0;j<3;j++)
{
if(j==0)
{
data[i][0]=Integer.toString(i+1);
}else if(j==1)
{
//System.out.println("In Model:");
//System.out.println(" &&&&&&"+p.getUserName());
data[i][1]=p.getUserName();
}else if(j==2)
{
// System.out.println("p.getScore"+p.getScore());
data[i][2]=Integer.toString(p.getScore());
}
}
}
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row,int col)
{
return data[row][col];
}
public boolean isCellEditable(int row,int col)
{
if(col!=1)
{
return false;
}else
{
if(data[row][1].equals(""))
return true;
else
return false;
}
}
public void setValueAt(Object value,int row,int col)
{
data[row][col]=value;
fireTableCellUpdated(row,col);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -