try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:oradsn","mahesh","genius");
Statement stmt = conn.createStatement();
ResultSet rs= stmt.executeQuery("select count(*) from std");
rs.next();
int maxno= Integer.parseInt(rs.getString(1));
String[] ColumnNames = {"Roll No","Name","English","Maths","Computer"};
Object[][] rowdata=new Object[maxno][5];
Statement stmt1 = conn.createStatement();
ResultSet rs1= stmt1.executeQuery("select * from std");
int r=0;
while(rs1.next()) {
rowdata[r][0]=rs1.getString(1);
rowdata[r][1]=rs1.getString(2);
rowdata[r][2]=rs1.getString(3);
rowdata[r][3]=rs1.getString(4);
rowdata[r][4]=rs1.getString(5);
r++;
}
JTable table = new JTable(rowdata,ColumnNames);
jPanel1.add(table.getTableHeader(), BorderLayout.NORTH);
jPanel1.setLayout(new FlowLayout());
jPanel1.add(new JScrollPane().add(table));
this.pack();
}
在此代码中,我希望ColumnNames
位于表格之上。但是当我运行程序时,左边有ColumnNames
,右边有桌子。
我认为问题出在最后5行。请帮忙。
答案 0 :(得分:2)
删除
jPanel1.add(table.getTableHeader(), BorderLayout.NORTH);
然后改变
jPanel1.add(new JScrollPane().add(table));
到
jPanel1.add(new JScrollPane(table));
我可能也会改变
jPanel1.setLayout(new FlowLayout());
到
jPanel1.setLayout(new BorderLayout());
还要记住,一个容器有一个布局管理器,如果你改变它,它会影响到之前添加到它的其他组件
我们建议您查看How to use Scroll Panes和How to use tables,因为它们提供了有关如何使用它们的更多信息