Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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
GridBagLayout的Java问题+;网格布局_Java_Swing_Layout_Panel_Gridbaglayout - Fatal编程技术网

GridBagLayout的Java问题+;网格布局

GridBagLayout的Java问题+;网格布局,java,swing,layout,panel,gridbaglayout,Java,Swing,Layout,Panel,Gridbaglayout,我在GridBagLayout中的GridBagLayout有问题 我希望我的GridLayout适合GridBagLayout的宽度 我举例说明了我要做的事情: 但我得到的是: “Test,version:1.5,disposible”是一个包含1列和3行的GridLayout。 “Mitramail,1.0版,可显示”是另一个网格布局,也有1列3行。 我正在尝试将它们添加到另一个具有x行和2列的GridLayout中。所以通常它们应该挨在一起,但它们是一个接一个的 代码如下: GridB

我在GridBagLayout中的GridBagLayout有问题

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

我举例说明了我要做的事情:

但我得到的是:

“Test,version:1.5,disposible”是一个包含1列和3行的GridLayout。 “Mitramail,1.0版,可显示”是另一个网格布局,也有1列3行。 我正在尝试将它们添加到另一个具有x行和2列的GridLayout中。所以通常它们应该挨在一起,但它们是一个接一个的

代码如下:

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

}
GridBagConstraints c=新的GridBagConstraints();
c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=1;
c、 锚点=GridBagConstraints.WEST;
c、 权重x=1;
panelDynamique.add(新的JLabel(“可争议”),c);
c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=2;
c、 填充=GridBagConstraints.HORIZONTAL;
add(新的jsepator(),c);
panelDispo.setLayout(新的GridLayout(这个.getNbAppFor(“Disponible”)/*变量X,为了更好地理解它,用2替换*/,2));
c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=3;
//c、 填充=GridBagConstraints.HORIZONTAL;
c、 锚点=GridBagConstraints.NORTH;
c、 权重=1;
panelDynamique.add(panelDispo,c);
c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=4;
c、 填充=GridBagConstraints.HORIZONTAL;
add(新的jsepator(),c);
for(条目:this.HMServ().entrySet()){
JPanel tmp=新的JPanel();
tmp.setLayout(新网格布局(3,1));
add(新JLabel(entry.getKey());//1)Test,2)Mitramail
添加(新JLabel(“版本:“+entry.getValue());//1)版本1.5,2)1.0
tmp.add(新的JLabel(this.infoVersion(entry.getKey(),entry.getValue()));//可显示
面板显示添加(tmp);
}
任何人都知道如何使我的“绿色”GridLayout填充红色GridBagLayout宽度,并有2列添加动态GridLayout,如下图所示:

1 | 2 3 | 4 5 | 6 7 |

?


感谢您的关注。

诊断上下文之外的代码片段有点困难,因为您的代码中可能有一些未共享的内容,这些内容可能会影响结果

在将来,考虑提供一个说明你的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应

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


啊,垃圾袋。谢谢你的回答。我用你的代码增强了我的代码,现在它几乎可以工作了。谢谢!编辑:现在终于开始工作了!谢谢你的帮助!
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);
                }
            }
        }

    }

}