Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中的网格布局_Java_User Interface_Layout_Gridbaglayout - Fatal编程技术网

JAVA中的网格布局

JAVA中的网格布局,java,user-interface,layout,gridbaglayout,Java,User Interface,Layout,Gridbaglayout,我想使用GridBagLayout显示图像、JLabel和JTextField 图像应该是这样的 // 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.grid

我想使用
GridBagLayout
显示图像、JLabel和JTextField

图像应该是这样的

 // 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);

这是我尝试过的,但是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);
    }
JPanel panel=newjpanel(newgridbagloayout());
gbc=新的GridBagConstraints();
for(int i=0;i
问题在于,对
gridy
的值进行加减是错误的。要将代码调整为
qtyField
位于
foodLabel
的右侧,只需删除那些
gbc.gridy
,并在
面板.add(foodLabel[i],gbc)之后添加一个
gbc.gridx++

现在,它不会像你的图像那样(至少我认为它不会,除非你正确设置
食物标签
,即使你这样做了,正确对齐也会是一个小到多的工作),要做到这一点,你可以创建一个新的
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);
}
JPanel panel=newjpanel(newgridbagloayout());
gbc=新的GridBagConstraints();
//只是为了在组件之间留出空间
gbc.插图=新插图(5,5,5,5);
for(int i=0;i

问题在于,您对
gridy
的值进行加减是错误的。要将代码调整为
qtyField
位于
foodLabel
的右侧,只需删除那些
gbc.gridy
,并在
面板.add(foodLabel[i],gbc)之后添加一个
gbc.gridx++

现在,它不会像你的图像那样(至少我认为它不会,除非你正确设置
食物标签
,即使你这样做了,正确对齐也会是一个小到多的工作),要做到这一点,你可以创建一个新的
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);
}
JPanel panel=newjpanel(newgridbagloayout());
gbc=新的GridBagConstraints();
//只是为了在组件之间留出空间
gbc.插图=新插图(5,5,5,5);
for(int i=0;i

您应该像这样更改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);
//使用-2初始化gridy,因为当i==0时,在for循环中将它增加2
gbc.gridy=-2;
gbc.gridx=0;
for(int i=0;i

。。。最后一个问题:为什么要将gridy增加2?将其增加1还不够吗?

您应该像这样更改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);
//使用-2初始化gridy,因为当i==0时,在for循环中将它增加2
gbc.gridy=-2;
gbc.gridx=0;
for(int i=0;i

。。。最后一个问题:为什么要将gridy增加2?增加1难道还不够吗?

图片上的JTextField是什么?“蓝色盒子?”弗洛里安。是的,你的pi是什么