滚动条和按钮将被绘制,但不会绘制表格中的单元格,并且控制台已正确打印数据,所以我想知道我哪里做错了。
public class MyGUIManager extends JPanel {
protected JFrame frame;
protected JScrollPane scrollPane=null;
protected JTable table=null;
protected TableModel model=null;
protected JButton btnInsert=null;
protected String dbname,tblname;
protected Vector<String> colNames;
protected Vector<Vector<String>> data,backup;
protected int col;
protected MySQLGUIManager() throws Exception{
frame =new JFrame("MySQL Manager");
frame.setContentPane(this);
frame.setMinimumSize(new Dimension(800,600));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
init();
}
protected void init() throws Exception{
//some inrrelevant operations about database
loadTbl();
}
protected void loadTbl() throws SQLException{
table=new JTable(data,colNames){
private static final long serialVersionUID = 1L;
public void tableChanged(TableModelEvent e){
int column=e.getColumn();
assert e.getFirstRow()==e.getLastRow():"more than 1 row have been changed!";
int row=e.getFirstRow();
if(scrollPane!=null){
//some codes
}
}
};
model=table.getModel();
scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrollPane,BorderLayout.CENTER);
btnInsert=new JButton("Insert a row");
add(btnInsert,BorderLayout.SOUTH);
repaint();
revalidate();
// scrollPane.repaint();
// scrollPane.revalidate();
System.out.println(model.getColumnCount()+" "+model.getRowCount());
for(int i=0;i<data.size();i++){
for(int j=0;j<data.get(0).size();j++){
System.out.print(model.getValueAt(i, j)+" ");
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
new MySQLGUIManager();
}
}
(省略了一些代码,但它们与 GUI 无关。)提前致谢!