62,614
社区成员
发帖
与我相关
我的任务
分享
public void recordFrame(final Connection conn) {
this.setTitle("寝室财产管理系统1.0--收支记录");
this.setSize(WIDTH, HEIGHT);
this.setLocation(x, y);
this.setBackground(Color.YELLOW);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
this.setVisible(true);
this.setResizable(false);
}
TableModel tModel = new AbstractTableModel() {
//加入此方法,否则JTABLE无法正确判值的类型
public Class getColumnClass(int column) {
return java.lang.String.class;
}
}
public void recordFrame(final Connection conn) {
this.setTitle("寝室财产管理系统1.0--收支记录");
this.setSize(WIDTH, HEIGHT);
this.setLocation(x, y);
this.setBackground(Color.YELLOW);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
this.setVisible(true);
this.setResizable(false);
//向窗口中绘制一张表格
Statement stmt = DB.createStmt(conn);
String sql = "select ROW_NUMBER() OVER (order by 日期 desc) as 编号,用途,类型,金额,日期 from 寝费";
ResultSet rs = DB.executeQuery(stmt, sql);
final ArrayList<String> num = new ArrayList<String>();
final ArrayList<String> use = new ArrayList<String>();
final ArrayList<String> type = new ArrayList<String>();
final ArrayList<String> money = new ArrayList<String>();
final ArrayList<String> date = new ArrayList<String>();
try {
while(rs.next()) {
num.add(Integer.toString(rs.getInt("编号")));
use.add(rs.getString("用途"));
type.add(rs.getString("类型"));
money.add(rs.getString("金额"));
date.add(rs.getString("日期"));
}
} catch (SQLException e2) {
e2.printStackTrace();
}
DB.close(rs);
DB.close(stmt);
TableModel tModel = new AbstractTableModel() {
//拿到有多少条记录
public int rowCount() {
Statement stmtCount = DB.createStmt(conn);
String sqlCount = "select count(*) as 行数 from 寝费";
ResultSet rsCount = DB.executeQuery(stmtCount, sqlCount);
int count = 0;
try {
rsCount.next();
count = rsCount.getInt("行数");
} catch (SQLException e1) {
e1.printStackTrace();
}
DB.close(rsCount);
DB.close(stmtCount);
return count;
}
//表格的行数
public int getRowCount() {return rowCount();}
//表格的列数
public int getColumnCount() {return 5;}
public Object getValueAt(int row, int col) {
String[][] s = new String[rowCount()][6];
for(row=1; row<=rowCount(); row++) {
for(col=1; col<=5; col++) {
switch(row) {
case 1:
s[row][col] = num.get(row);
break;
case 2:
s[row][col].valueOf(use.get(row));
break;
case 3:
s[row][col] = type.get(row).toString();
break;
case 4:
s[row][col] = money.get(row).toString();
break;
case 5:
s[row][col] = date.get(row).toString();
break;
}
}
}
return s;
}
//设置表格的列名
public final String[] columnName = {"编号", "用途", "类型", "金额(元)", "日期"};
public String getColumnName(int column) {
return columnName[column];
}
};
JTable table = new JTable(tModel);
JScrollPane scrollpane = new JScrollPane(table);
this.add(scrollpane);
}