谢谢各位,我弄了一个,如下
class RecordList extends JFrame implements ActionListener
{
JTable jt=null; //表格
Container c=this.getContentPane();
JButton jb1=new JButton("清除记录");
JButton jb2=new JButton("关闭");
JPanel jp=new JPanel();
BorderLayout bl=new BorderLayout();
Connection con=null;
Statement st=null;
ResultSet rs=null;
public RecordList()
{
super("排行榜");
c.setLayout(new FlowLayout());
c.setBackground(new Color(29,250,180));
jp.setLayout(bl);
String[] title={"姓名","次数"};
String[][] data={{null},{null} };
jt=new JTable(6,2);
jt.setRowHeight(30);
//jp.add(jt.getTableHeader(),BorderLayout.NORTH);
jp.add(jt,BorderLayout.CENTER);
c.add(jp);
c.add(jb1);
c.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:xx");
st = con.createStatement();
rs=st.executeQuery("select top 5 * from record order by gsum asc");
int i=0;
while(rs.next())
{
i++;
String str1=rs.getString(1);
String str2=rs.getString(2);
jt.setValueAt(str1,i-1,0);
jt.setValueAt(str2,i-1,1);
}
st.close();
st = null;
con.close();
con = null;
}catch(Exception ex) { ex.printStackTrace();}
this.setSize(220,280);
this.setLocation(200,200);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(jb1))
{
...
}