【问题标题】:GridBagLayout in JAVAJAVA中的GridBagLayout
【发布时间】:2017-11-28 14:36:29
【问题描述】:

我想使用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);
    }

【问题讨论】:

  • 你的图片上的JTextField是什么?蓝色的盒子?
  • @FlorianS。是的。

标签: java user-interface layout gridbaglayout


【解决方案1】:

问题是你加减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);
}

I ran this code, and should be like this

【讨论】:

  • 如何增加JTextField的宽度?
  • 您必须创建一个新的 Dimension 实例并在 jTextField.setPreferredSize(/*dimension instance*/) 中使用它。您也可以根据需要使用setSize()setMaximumSize()setMinimunSize()。查看tutorialdocumentation,均由 Oracle 提供。
【解决方案2】:

你应该像这样改变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应该不够吧?

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2011-07-23
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多