GridLayout的Java问题

GridLayout的Java问题

本文介绍了GridBagLayout + GridLayout的Java问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridBagLayout中使用GridLayout遇到麻烦.

I'm having trouble with a GridLayout in a GridBagLayout.

我希望我的GridLayout适合GridBagLayout的宽度.

I want my GridLayout fit the width of the GridBagLayout.

我说明了我要做什么:

但这是我获得的:

测试,版本:1.5,不合格"是具有1列和3行的GridLayout."Mitramail,1.0版,Disponible"是另一个具有1列和3行的GridLayout.我试图将它们添加到具有x行和2列的另一个GridLayout中.因此通常它们应该彼此相邻,但是彼此之间却是一个.

"Test, version : 1.5, Disponible" is a GridLayout with 1 columns and 3 rows."Mitramail, version 1.0, Disponible" is another GridLayout with 1 columns and 3 rows too.I'm trying to adding them in another GridLayout with x row and 2 columns. so normally they should be next to each other, but they are one under the other.

这是代码:

GridBagConstraints c = new GridBagConstraints();
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.weightx = 1;
panelDynamique.add(new JLabel("Disponible"), c);

c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panelDynamique.add(new JSeparator(), c);

panelDispo.setLayout(new GridLayout(this.getNbAppFor("Disponible") /*Variable X, replace by 2 for better understanding*/, 2));

c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 3;
//c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
c.weighty = 1;
panelDynamique.add(panelDispo, c);

c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
panelDynamique.add(new JSeparator(), c);

for(Entry<String, String> entry : this.HMServ().entrySet()){

    JPanel tmp = new JPanel();
    tmp.setLayout(new GridLayout(3, 1));
    tmp.add(new JLabel(entry.getKey())); //1) Test, 2) Mitramail
    tmp.add(new JLabel("Version : " + entry.getValue())); //1) Version 1.5, 2) 1.0
    tmp.add(new JLabel(this.infoVersion(entry.getKey(), entry.getValue()))); //Disponible

    panelDispo.add(tmp);

}

任何人都知道如何使我的绿色" GridLayout填充红色GridBagLayout宽度,并有2列添加如下所示的动态GridLayout:

Anybody knows how can i make my "Green" GridLayout fill the Red GridBagLayout width and have 2 columns to add dynamics GridLayout like this schema :

7 | ...

?

感谢您的关注.

推荐答案

根据上下文代码段很难诊断,因为您的代码中可能存在某些事情,您尚未共享,可能是影响结果

It's a little hard to diagnose out of context code snippets, as there might be things going on in your code, which you've not shared, which might be effecting the results

将来,请考虑提供一个可运行的示例,以证明您的问题.这不是代码转储,而是您正在执行的操作的一个示例,突出显示了您所遇到的问题.这样可以减少混乱并获得更好的响应

In the future, consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses

话虽如此,它似乎对我有用...

Having said that, it seems to work for me...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class MakeItSo {

    public static void main(String[] args) {
        new MakeItSo();
    }

    public MakeItSo() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {
            setLayout(new GridBagLayout());
            setBorder(new LineBorder(Color.RED));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(new JLabel("Disponible:"), gbc);
            add(new JSeparator(), gbc);
            add(new GridPane());
        }

    }

    public class GridPane extends JPanel {

        public GridPane() {
            setLayout(new GridLayout(2, 2));
            setBorder(new LineBorder(Color.YELLOW));

            add(new DetailPane("Mitramail.zip", "Version: 1.0", "Disponible"));
            add(new DetailPane("Test", "Version: 1.5", "Disponible"));
            add(new DetailPane("Other"));
            add(new DetailPane("Other"));
        }

    }

    protected class DetailPane extends JPanel {

        public DetailPane(String... values) {
            setLayout(new GridBagLayout());
            setBorder(new LineBorder(Color.GREEN));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            if (values != null && values.length > 0) {
                for (String value : values) {
                    add(new JLabel(value), gbc);
                }
            }
        }

    }

}

这篇关于GridBagLayout + GridLayout的Java问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:42