Java GridBagLayout和JPanel错误:无法添加到布局:约束必须为字符串(或为null) [英] Java GridBagLayout and JPanel Error: cannot add to layout: constraint must be a string (or null)
问题描述
我已经研究了此错误,但似乎找不到解决方法.我试图创建一个包含40列20行的800个JButton的网格.最终,它将用于控制我正在制作的多米诺骨牌设置机器人,该机器人将翻倒多米诺骨牌.我已经使用GridLayout成功创建了一个网格,但是由于项目的性质,我希望每隔一行用半个按钮偏移一次.我的意思是像设置计算机键盘上的键一样. (我会为我要解释的内容添加一张有用的图片,但是显然,在解释方面有麻烦的初学者无论如何都不允许添加图片.)
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).
我尝试通过创建一个包含20个面板的JPanel数组(称为panel)来做到这一点.然后,将40个JButton添加到面板中.然后,我使用GridBagConstraints偏移每隔一行.我读到,您不应该混合使用awt和swing,这样可能是问题所在,但我不知道.这是代码,我是YouTube初学者,因此我从youtube教程中弄清楚了.如果我说的话没有道理,请原谅我.代码:
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:
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)
请帮助我找出问题并解决.谢谢
Please help me figure out the problem and get it working. Thanks
edit:我仍然对确切如何使用gridbagconstraints感到困惑,因此我什至不知道gridy和gridx是否适合在这里使用.或者即使我应该使用gridbagconstraints.请提供任何建议以完成工作.谢谢
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
这似乎起作用.
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();
}
}
推荐答案
您这样做:
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);
}
}
这篇关于Java GridBagLayout和JPanel错误:无法添加到布局:约束必须为字符串(或为null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!