Java GridBagLayout和JPanel错误:无法添加到布局:约束必须是字符串(或null)

时间:2013-05-12 05:39:31

标签: java swing jpanel layout-manager gridbaglayout

我已经研究过这个错误,我似乎无法找到解决方案。我正在尝试创建一个包含40列和20行的800 JButtons网格。这将最终用于控制我正在制作的多米诺设置机器人将提示多米诺骨牌。我已经使用GridLayout成功创建了一个网格,但由于项目的性质,我希望每隔一行偏移半个按钮。我的意思是如何设置计算机键盘上的键。 (我本来会添加一张有用的图片,说明我要解释的内容,但显然无法解释事情的初学者不允许添加图片,无论如何)。

我尝试通过创建一个名为panel的20个面板的JPanel数组来做到这一点。然后我在面板上添加了40个JButtons。然后我使用GridBagConstraints来抵消每隔一行。我读到你不应该混合awt和swing这样可能是问题,但我不知道。这是代码,我从youtube教程中找到了这个,因为我是一个非常初学者。如果我说的话没有意义,请原谅我。代码:

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
public class OffsetGrid {

    public static void main (String [] args){
        JFrame Frame = new JFrame();
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout grid= new GridLayout();


        GridBagConstraints gbca= new GridBagConstraints();
        GridBagConstraints gbcb= new GridBagConstraints();
        JPanel[] panel=new JPanel[20];


        for (int row=0;row<20; row++){
                panel[row]=new JPanel(new GridBagLayout()); 
                gbca.gridx=1;
                gbca.gridy=row;
                gbcb.gridx=0;
                gbcb.gridy=row;

        for (int y=0; y<40;y++){
                grid=new GridLayout(1,40);
                panel[row].setLayout(grid);
                JButton[] button = new JButton[40];
                button[y]=new JButton();
                button[y].setOpaque(true);
                panel[row].add(button[y]);

                }
            if (row%2==0){
                Frame.add(panel[row], gbcb);
            }
            else {
                Frame.add(panel[row], gbca);
            }

    }

        Frame.setVisible(true);
        Frame.setLocationRelativeTo(null);
        Frame.pack();
}
}

错误:

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426)
    at javax.swing.JRootPane$1.addLayoutComponent(JRootPane.java:531)
    at java.awt.Container.addImpl(Container.java:1120)
    at java.awt.Container.add(Container.java:998)
    at javax.swing.JFrame.addImpl(JFrame.java:562)
    at java.awt.Container.add(Container.java:966)
    at OffsetGrid.main(OffsetGrid.java:38)

请帮我弄清楚问题并让它正常工作。感谢

编辑:我仍然对如何使用gridbagconstraints感到困惑,所以我甚至不知道gridy和gridx是否适合在这里使用。或者即使我应该使用gridbagconstraints。请提供任何建议来完成工作。感谢

编辑:这似乎有效。

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
public class OffsetGrid {

    public static void main (String [] args){
        JFrame Frame = new JFrame();
        Frame.setLayout(new GridBagLayout());
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout grid= new GridLayout();


        GridBagConstraints gbca= new GridBagConstraints();
        GridBagConstraints gbcb= new GridBagConstraints();
        JPanel[] panel=new JPanel[20];


        for (int row=0;row<20; row++){
                panel[row]=new JPanel(new GridBagLayout()); 
                gbca.insets=new Insets(0,100,0,0);
                gbca.gridy=row;
                gbcb.insets=new Insets(0,0,0,0);
                gbcb.gridy=row;

        for (int y=0; y<40;y++){
                grid=new GridLayout(1,40);
                panel[row].setLayout(grid);
                JButton[] button = new JButton[40];
                button[y]=new JButton();
                button[y].setOpaque(true);
                panel[row].add(button[y]);

                }
            if (row%2==0){
                Frame.add(panel[row], gbcb);
            }
            else {
                Frame.add(panel[row], gbca);
            }

    }

        Frame.setVisible(true);
        Frame.setLocationRelativeTo(null);
        Frame.pack();
}
}

1 个答案:

答案 0 :(得分:6)

你这样做了:

Frame.add(panel[row], gbcb);

但是你忘了设置框架的布局:

JFrame Frame = new JFrame();
Frame.setLayout(new GridBagLayout());

现在不会抛出异常。然而......

Outcome

我想知道这是不是你想要的:

My effort

在用户调整窗口大小的情况下,我对代码进行了一些更改。您仍然可以在revision

中找到旧代码
import static javax.swing.BorderFactory.createEmptyBorder;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class OffsetGrid {
    public static final int ROWS = 10;
    public static final int COLUMNS = 10;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(ROWS, 1));
        final JPanel[] panel = new JPanel[ROWS];
        final JButton[][] button = new JButton[ROWS][COLUMNS];
        for (int row = 0; row < ROWS; row++) {
            panel[row] = new JPanel(new GridLayout(1, COLUMNS));
            for (int y = 0; y < COLUMNS; y++) {
                button[row][y] = new JButton(row + "-" + y);
                button[row][y].setOpaque(true);
                panel[row].add(button[row][y]);
            }
            int padding = button[row][0].getPreferredSize().width / 2;
            if (row % 2 == 0)
                panel[row].setBorder(createEmptyBorder(0, 0, 0, padding));
            else
                panel[row].setBorder(createEmptyBorder(0, padding, 0, 0));
            frame.add(panel[row]);
        }
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                for (int row = 0; row < ROWS; row++) {
                    int padding = button[row][0].getSize().width / 2;
                    panel[row].setBorder(createEmptyBorder(0, 0, 0, padding));
                    if (++row == ROWS)
                        break;
                    padding = button[row][0].getSize().width / 2;
                    panel[row].setBorder(createEmptyBorder(0, padding, 0, 0));
                }
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}