【发布时间】:2015-08-05 12:22:35
【问题描述】:
我的网格包布局有问题,我试图让图像水平排在一条线上,但是当我将图像添加到原始图像的左侧时,它会垂直下降而不是水平下降到左边。
我尝试将 gridY 指定为相同,但这不起作用,我也尝试过 GridBayConstarints.Horizontal 但这无关紧要。
到现在为止
import java.awt.GridBagConstraints;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class main {
public static void main(String[] args)
{
GUI g = new GUI();
int turnCounter = 1;
for(int i = 0; i<6; i++)
{
String image = "src/resources/bone"+0+i+".png";
GridBagConstraints c = new GridBagConstraints();
//ADDS IMAGE TO THE LEFT
if(turnCounter%2 == 0)
{
c.gridy = c.gridy;
c.gridx = c.gridx-1;
ImageIcon icon1 = new ImageIcon(image);
JLabel label1 = new JLabel((ImageIcon) icon1);
g.add(label1,c);
g.revalidate();
turnCounter++;
}
//ADD IMAGES TO THE RIGHT
else if(turnCounter%2 == 1)
{
c.gridy = c.gridy;
c.gridx = c.gridx+1;
ImageIcon icon1 = new ImageIcon(image);
JLabel label1 = new JLabel((ImageIcon) icon1);
g.add(label1,c);
g.revalidate();
turnCounter++;
}
}
}
}
图片现在的样子。 [2|0] 和 [3|0] 应该按顺序在 [0|0] 的左侧,所以它看起来像这样
[4|0][2|0][0|0][1|0][3|0][5|0] 都在一行中。
【问题讨论】:
标签: java gridbaglayout