Java - GridBagLayout预设列和行

时间:2017-05-23 07:32:54

标签: java swing user-interface gridbaglayout

我尝试使用GridBagLayout设置一个gui。要单独调整组件,我希望有一个默认的行和列数。所以我放入了我的JFrame面板,但是当我启动它时,它仍然有一些插图。有人能帮助我吗?

// Constraints for Layout
private GridBagConstraints gbc = new GridBagConstraints();
private final int COLUMNCOUNT = 100;
private final int ROWCOUNT = 60;
// Default Panel um GBL zu init
private JPanel jpHolder;

...

public LevelView() {
    init();
}

private void init() {
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridBagLayout());
    initGBL();

    pack();
    setVisible(true);
}

private void initGBL() {
    for (int x = 0; x < COLUMNCOUNT; x++) {
        for (int y = 0; y < ROWCOUNT; y++) {
            if (x == 0 || y == 0) {
                jpHolder = new JPanel();
                jpHolder.setSize(10, 10);
                jpHolder.setBorder(BorderFactory.createLineBorder(Color.black));
                gbc = new GridBagConstraints();
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridx = x;
                gbc.gridy = y;
                add(jpHolder, gbc);
            } else {
                jpHolder = new JPanel();
                jpHolder.setSize(10, 10);
                jpHolder.setBorder(BorderFactory.createLineBorder(Color.red));
                gbc = new GridBagConstraints();
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridx = x;
                gbc.gridy = y;
                add(jpHolder, gbc);
            }
        }
    }
}

这就是它的样子: Weird inset or whatever

0 个答案:

没有答案