环顾堆栈交换,我找不到一个好的答案。我见过的类似问题中的大多数代码看起来很庞大而且很臃肿,很容易出现错误。我看不出我是否犯了同样的错误。
我只有两个类(和一个接口),我的问题是我的 JTable 是空白的:
这是我的代码:
启动程序
public class Launcher {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
IViewManager.Util.getInstance();
}
});
}
}
启动 ViewManager 框架
public class ViewManager implements IViewManager {
JFrame frame = null;
JTabbedPane myListTabs = null;
ComicsListPane myComicsListPane = null;
public ViewManager(){
//Create and set up the window.
frame = new JFrame("My List Agregator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the Tabbed pane for lists.
myListTabs = new JTabbedPane();
myComicsListPane = new ComicsListPane();
myListTabs.add(myComicsListPane);
myListTabs.setTitleAt(myListTabs.getTabCount()-1, "title");
frame.getContentPane().add(myListTabs);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
漫画列表窗格
public class ComicsListPane extends JPanel {
/**
*
*/
private static final long serialVersionUID = -5207104867199042105L;
JTable myComicsTable = null;
public ComicsListPane() {
//Create the column names and data
String[] columnNames = {"Comic Title",
"Volume",
"Edition",
"Purchased"};
Object[][] data = {
{"The Amazing Spider-man", new Integer(3),
new Integer(679), new Boolean(false)},
{"The Amazing Spider-man", new Integer(3),
new Integer(680), new Boolean(false)}
};
//Create the table
myComicsTable = new JTable(data, columnNames);
myComicsTable.setPreferredScrollableViewportSize(new Dimension(750, 110));
myComicsTable.setFillsViewportHeight(true);
myComicsTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(myComicsTable);
scrollPane.add(myComicsTable);
scrollPane.setPreferredSize(new Dimension(450, 110));
this.add(scrollPane, BorderLayout.CENTER);
}
}
我看不出哪里出错了,主要是我从 Java Doc Tutorials 中复制过来的。
有人可以向java新手指出错误吗?