在 JPanel 中获得良好的网格布局
Getting a good grid layout in a JPanel
我正在做一个项目,在这个项目中我在一个简单的软件中模拟(Conway 的)生命游戏。后端部分已完成,但我在很好地显示单元格时遇到了一些麻烦。我想要实现的是拥有 y 行和 x 列(x 和 y 可能因模拟而异)。每个 cel 可以是活的(某种颜色)或死的(另一种颜色),我希望它们是正方形或至少接近正方形。我还需要能够修改特定的单元格,例如第 5 行第 3 列的单元格可能需要在不影响其他单元格的情况下变成不同的颜色。
由于这些要求,我目前正在使用 GridBagLayout,但没有得到想要的结果。我面临两个问题:
- 我只能得到一行,而我可以修改我有多少列
- 我不知道如何制作每个单元格的正方形,它们有时会被拉伸 horizontally/vertically
我目前的代码,在我得到正确的形状之前,我一直在胡闹:
//Set grid dimensions
int gridSizeX = 5;
int gridSizeY = 5;
JFrame f;
JPanel p;
//Set the frame
f = new JFrame("Window");
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the panel
p = new JPanel();
p.setLayout(new GridBagLayout());
p.setBackground(Color.blue);
//Set the constraints
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
//The panel that will hold the grid
JPanel a;
//Cycle through all fields of the grid and alternate between red and yellow fields
for (int i = 0; i < gridSizeX; i++)
{
for (int j = 0; j < gridSizeY; j++)
{
a = new JPanel();
if (i%2==0)
a.setBackground(Color.yellow);
else
a.setBackground(Color.red);
c.gridx = i;
c.gridy = j;
p.add(a, c);
}
}
//Make sure the frame and panel are shown
f.add(p);
f.setVisible(true);
你设置每个方块颜色的逻辑是错误的。你不能只做一个简单的 "i % 2"。这只适用于偶数行。您需要翻转奇数行的逻辑。
因此,您需要进行额外检查以确定将正方形添加到哪一行。所以逻辑是这样的:
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
JPanel square = new JPanel( new BorderLayout() );
square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
chessBoard.add( square );
}
}
I'm currently working with a GridBagLayout
只需使用一个GridLayout
。无需担心约束。
- 您获得了更多行,但不同行的列颜色相同。因此,您看不到区别。使用
(i+j)%2
- 除了
c.weightx = 0.5
你还应该设置c.weighty = 0.5
我正在做一个项目,在这个项目中我在一个简单的软件中模拟(Conway 的)生命游戏。后端部分已完成,但我在很好地显示单元格时遇到了一些麻烦。我想要实现的是拥有 y 行和 x 列(x 和 y 可能因模拟而异)。每个 cel 可以是活的(某种颜色)或死的(另一种颜色),我希望它们是正方形或至少接近正方形。我还需要能够修改特定的单元格,例如第 5 行第 3 列的单元格可能需要在不影响其他单元格的情况下变成不同的颜色。
由于这些要求,我目前正在使用 GridBagLayout,但没有得到想要的结果。我面临两个问题:
- 我只能得到一行,而我可以修改我有多少列
- 我不知道如何制作每个单元格的正方形,它们有时会被拉伸 horizontally/vertically
我目前的代码,在我得到正确的形状之前,我一直在胡闹:
//Set grid dimensions
int gridSizeX = 5;
int gridSizeY = 5;
JFrame f;
JPanel p;
//Set the frame
f = new JFrame("Window");
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the panel
p = new JPanel();
p.setLayout(new GridBagLayout());
p.setBackground(Color.blue);
//Set the constraints
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
//The panel that will hold the grid
JPanel a;
//Cycle through all fields of the grid and alternate between red and yellow fields
for (int i = 0; i < gridSizeX; i++)
{
for (int j = 0; j < gridSizeY; j++)
{
a = new JPanel();
if (i%2==0)
a.setBackground(Color.yellow);
else
a.setBackground(Color.red);
c.gridx = i;
c.gridy = j;
p.add(a, c);
}
}
//Make sure the frame and panel are shown
f.add(p);
f.setVisible(true);
你设置每个方块颜色的逻辑是错误的。你不能只做一个简单的 "i % 2"。这只适用于偶数行。您需要翻转奇数行的逻辑。
因此,您需要进行额外检查以确定将正方形添加到哪一行。所以逻辑是这样的:
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
JPanel square = new JPanel( new BorderLayout() );
square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
chessBoard.add( square );
}
}
I'm currently working with a GridBagLayout
只需使用一个GridLayout
。无需担心约束。
- 您获得了更多行,但不同行的列颜色相同。因此,您看不到区别。使用
(i+j)%2
- 除了
c.weightx = 0.5
你还应该设置c.weighty = 0.5