Java GridBagLayout 和 JPanel 错误:无法添加到布局:约束必须是字符串(或空值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16504538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java GridBagLayout and JPanel Error: cannot add to layout: constraint must be a string (or null)
提问by user2278269
I have researched this error and I cannot seem to find a solution. I am trying to create a grid of 800 JButtons with 40 columns and 20 rows. This will eventually be used to control a domino setting up robot I am making that will tip over dominoes. I have already successfully created a grid using GridLayout, but due to the nature of the project, I would like every other row to be offset by half a button. By this I mean like how keys on a computer keyboard are set up. (I would have added a helpful picture of what I am trying to explain, but apparently beginners who have trouble explaining things aren't allowed to add pictures, whatever).
我已经研究了这个错误,但似乎找不到解决方案。我正在尝试创建一个包含 800 个 JButton 的网格,其中包含 40 列和 20 行。这最终将用于控制我正在制作的多米诺骨牌设置机器人,该机器人将翻倒多米诺骨牌。我已经使用 GridLayout 成功创建了一个网格,但由于项目的性质,我希望每隔一行都偏移半个按钮。我的意思是像计算机键盘上的键是如何设置的。(我会添加一张有用的图片来说明我要解释的内容,但显然无法解释事物的初学者不允许添加图片,无论如何)。
I try to do this by creating a JPanel array of 20 panels called panel. Then I add to the panel the 40 JButtons. Then I use GridBagConstraints to offset every other row. I read that you shouldn't mix awt and swing so that could be the problem, but i don't know. Here is the code, I figured this out from youtube tutorials as I am a very beginner. Forgive me if anything I have said does not make sense. Code:
我尝试通过创建一个名为 panel 的 20 个面板的 JPanel 数组来做到这一点。然后我将 40 个 JButton 添加到面板中。然后我使用 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();
}
}
error:
错误:
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.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)
Please help me figure out the problem and get it working. Thanks
请帮助我找出问题并使其正常工作。谢谢
edit: I am still confused on exactly how to use gridbagconstraints so I don't even know if gridy and gridx are even the right things to use here. Or even if i should use gridbagconstraints. Please offer any suggestions to get the job done. Thanks
编辑:我仍然对如何使用 gridbagconstraints 感到困惑,所以我什至不知道 gridy 和 gridx 是否适合在这里使用。或者即使我应该使用 gridbagconstraints。请提供任何建议以完成工作。谢谢
edit: this seems to work.
编辑:这似乎有效。
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();
}
}
回答by johnchen902
You did this:
你这样做了:
Frame.add(panel[row], gbcb);
But you forgot to set the frame's layout:
但是您忘记设置框架的布局:
JFrame Frame = new JFrame();
Frame.setLayout(new GridBagLayout());
Now the exception is not thrown. However......
现在不抛出异常。然而......
I wonder if this is what you want:
我想知道这是否是你想要的:
I've made some change to my code in the case that user resize the window. You can still find the older code in 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);
}
}