围绕堆栈交换我无法找到一个好的答案.我见过的类似问题中的大多数代码看起来都很庞大而且容易出错; 我不知道我是否犯了同样的错误.
我只有两个类(和一个接口),我的问题是我的JTable是空白的:

这是我的代码:
public class Launcher {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
IViewManager.Util.getInstance();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法看到我出错的地方,主要是我已经复制了Java Doc Tutorials.
有人可以帮助指出错误,给java新手?
看起来你的问题是你创建的时候JScrollPane.
JScrollPane scrollPane = new JScrollPane(myComicsTable);
scrollPane.add(myComicsTable);
Run Code Online (Sandbox Code Playgroud)
这里不需要第二行 - 你已经构建了JScrollPanewith JTable,没有必要再将它添加到JScrollPane.确实 - 这是使用add()来自的方法Container.如果没有更详细地查看javadocs/source,我究不清楚它导致你所看到的行为的原因并不清楚.
顺便说一句,当您使用GUI(实际上,通常)时,最好将代码缩小到最小的块.即创建的方法JTable- 可以显示吗?那么,一个将它包装在JScrollPane- 的方法可以按照你的意愿显示吗?等等
| 归档时间: |
|
| 查看次数: |
992 次 |
| 最近记录: |