繁体   English   中英

如何将JTable添加到JPanel

[英]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();
  }
}

如果有人有一些线索,那就太好了。

预先感谢。

  1. 添加组件以允许布局管理器完成其工作后,不要调用setSize()而是调用pack()
  2. 首先将JTable添加到JScrollPane,然后将其添加到GUI。
  3. 最重要的是, 添加所有组件之前,不要在JFrame上调用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM