问题
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).
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();
}
}
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$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: 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
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();
}
}
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/16504538/java-gridbaglayout-and-jpanel-error-cannot-add-to-layout-constraint-must-be-a