JAVA 中的 GridBagLayout
GridBagLayout in JAVA
我想使用 GridBagLayout
显示图像、JLabel 和 JTextField。
图片应该是这样的
这是我试过的,但是 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);
}
问题是你加减gridy
的值错了。要将代码调整为 qtyField
在 foodLabel
的右侧,只需删除那些 gbc.gridy
并在 panel.add(foodLabel[i], gbc)
.
之后添加一个 gbc.gridx ++
现在,它不会像你的图像一样(至少我认为它不会,除非你正确设置 foodLabel
,即使你这样做,正确对齐也会有一些工作),为此您可以创建一个 JLabel
(foodImage
) 的新数组,与 foodLabel
的大小相同,您可以在其中仅显示图像。那么你的代码几乎是正确的,只需要移动一些行并添加 foodImage
。我写了,这里是:
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);
}
你应该像这样更改 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);
...还有最后一个问题:为什么要将 gridy 增加 2?加1还不够吗?
我想使用 GridBagLayout
显示图像、JLabel 和 JTextField。
图片应该是这样的
这是我试过的,但是 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);
}
问题是你加减gridy
的值错了。要将代码调整为 qtyField
在 foodLabel
的右侧,只需删除那些 gbc.gridy
并在 panel.add(foodLabel[i], gbc)
.
gbc.gridx ++
现在,它不会像你的图像一样(至少我认为它不会,除非你正确设置 foodLabel
,即使你这样做,正确对齐也会有一些工作),为此您可以创建一个 JLabel
(foodImage
) 的新数组,与 foodLabel
的大小相同,您可以在其中仅显示图像。那么你的代码几乎是正确的,只需要移动一些行并添加 foodImage
。我写了,这里是:
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);
}
你应该像这样更改 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);
...还有最后一个问题:为什么要将 gridy 增加 2?加1还不够吗?