[英]How to add a JTable to a JPanel
我的问题似乎有些愚蠢,但是每次我使用秋千时,我都会遇到桌子问题。 所以我正在一个学校项目上工作,我需要使用GridBagLayout向JPanel添加一些JTable,但是我看不到将JTable添加到我的面板中。
这是代码:
public class MainView extends JFrame {
private static Dimension dimensionFenetre = new Dimension(1980, 1000);
Object[][] team = {
{"France", "80"},
{"Germany", "80"},
{"Italy", "80"},
{"England", "80"}
};
String titleColumn[] = {"Team", "Overall"};
public MainView() {
JPanel panelFenetre = new JPanel(new GridBagLayout());
add(panelFenetre);
setVisible(true);
panelFenetre.setVisible(true);
setSize(dimensionFenetre);
panelFenetre.add(getTable1(), getTable1Constraints());
}
private JTable getTable1() {
JTable table = new JTable(team, titleColumn);
table.setVisible(true);
return table;
}
private GridBagConstraints getTable1Constraints() {
GridBagConstraints gbcTable1 = new GridBagConstraints(
0, 1,
1, 1,
1, 1,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0);
return gbcTable1;
}
}
还有一个简单的Main:
public class Main {
public static void main(String[] args) {
MainView mainView = new MainView();
}
}
如果有人有一些线索,那就太好了。
预先感谢。
setSize()
而是调用pack()
。 setVisible(true)
。 对我来说效果很好:
public MainView() {
JPanel panelFenetre = new JPanel(new GridBagLayout());
add(panelFenetre);
// setVisible(true);
// panelFenetre.setVisible(true);
// setSize(dimensionFenetre);
panelFenetre.add(new JScrollPane(getTable1()), getTable1Constraints());
pack();
setLocationRelativeTo(null);
setVisible(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.