[英]GridBagLayout in JAVA
I want to display image, JLabel, and JTextField using GridBagLayout
. 我想使用GridBagLayout
显示图像,JLabel和JTextField。
The image should looked like this 图像应如下所示
This is what I have tried, but the JTextField display below images, not besides 这是我尝试过的方法,但JTextField显示在图像下方,除了
JPanel panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
for (int i = 0; i < ELEMENTS; i++) {
Image image = ImageIO.read(file[i]);
Image imageScaled = image.getScaledInstance(80, 95, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(imageScaled);
foodLabel[i] = new JLabel(imageIcon);
qtyField[i] = new JTextField(3);
}
gbc.gridx = 0;
for (int i = 0; i < ELEMENTS; i++) {
if (i % 3 == 0) {
gbc.gridy += 2;
gbc.gridx = 0;
}
panel.add(foodLabel[i], gbc);
gbc.gridy++;
panel.add(qtyField[i], gbc);
gbc.gridx++;
gbc.gridy--;
tabbedPane.addTab(text, panel);
}
The problem is that you are adding and subtracting the value of gridy
wrong. 问题是您要添加和减去gridy
错误的值。 To adjust the code to qtyField
be on the right side of foodLabel
, simply remove those gbc.gridy
and add a gbc.gridx ++
after panel.add(foodLabel[i], gbc)
. 要将代码调整为qtyField
右侧的foodLabel
,只需删除这些gbc.gridy
然后在panel.add(foodLabel[i], gbc)
之后添加一个gbc.gridx ++
panel.add(foodLabel[i], gbc)
。
Now, it won't be as your image (at least I think it wouldn't unless you properly setup the foodLabel
, and even if you do, align properly will be a little to much work), to do so you can create a new array of JLabel
( foodImage
), the same size of foodLabel
, where you can show only the images. 现在,它不会像您的图像一样(至少我认为除非您正确设置foodLabel
,否则它就不会foodLabel
,即使您这样做,正确对齐也将需要花费很多工作),为此,您可以创建一个新的JLabel
数组( foodImage
),具有相同的foodLabel
大小,在这里只能显示图像。 Then your code will be almost right, only move some lines and add the foodImage
. 然后,您的代码几乎正确,只需要移动几行并添加foodImage
。 I wrote and here it is: 我写的是:
JPanel panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
// Just to give a space between the components
gbc.insets = new Insets(5, 5, 5, 5);
for (int i = 0; i < ELEMENTS; i++) {
Image image = ImageIO.read(file[i]);
Image imageScaled = image.getScaledInstance(80, 95, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(imageScaled);
foodImage[i] = new JLabel(imageIcon);
foodLabel[i] = new JLabel("Label");
qtyField[i] = new JTextField(3);
}
gbc.gridx = 0;
for (int i = 0; i < ELEMENTS; i++) {
if (i % 3 == 0) {
gbc.gridy += 2;
gbc.gridx = 0;
}
// Add the image
panel.add(foodImage[i], gbc);
// Below the image
gbc.gridy++;
// Add the label
panel.add(foodLabel[i], gbc);
// Go back up
gbc.gridy--;
// Next column
gbc.gridx++;
// Add the textfield
panel.add(qtyField[i], gbc);
// Next column
gbc.gridx++;
tabbedPane.addTab(text, panel);
}
You should change the for loop like this 您应该像这样更改for循环
// initialize gridy with -2 because when i == 0 you increase it by 2 in the for loop
gbc.gridy = -2;
gbc.gridx = 0;
for (int i = 0; i < ELEMENTS; i++) {
if (i % 3 == 0) {
gbc.gridy += 2;
gbc.gridx = 0;
}
panel.add(foodLabel[i], gbc);
gbc.gridx++;
panel.add(qtyField[i], gbc);
gbc.gridx++;
// do you really whant to do this within the loop? I guess it should be outside...
// tabbedPane.addTab(text, panel);
}
tabbedPane.addTab(text, panel);
... and one last question: Why do you increase gridy by 2? ...还有最后一个问题:为什么将网格增加2? Shouldn't it be enough to increase it by 1? 将其增加1不够吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.