【问题标题】:Java issue with GridBagLayout + GridLayoutGridBagLayout + GridLayout 的 Java 问题
【发布时间】:2016-01-11 21:36:11
【问题描述】:

我在使用 GridBagLayout 中的 GridLayout 时遇到问题。

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

我做了一个说明我正在尝试做的事情:

但这是我得到的:

"Test, version : 1.5, Disponible" 是一个 1 列 3 行的 GridLayout。 “Mitramail,1.0 版,Disponible”是另一个 1 列和 3 行的 GridLayout。 我正在尝试将它们添加到另一个具有 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);

}

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

1 | 2

3 | 4

5 | 6

7 | ...

?

感谢您的关注。

【问题讨论】:

标签: java swing layout panel gridbaglayout


【解决方案1】:

诊断上下文代码 sn-ps 有点困难,因为您的代码中可能发生了一些您没有共享的事情,这可能会影响结果

以后,请考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应

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

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

    }

}

【讨论】:

  • 感谢您的回答。我用你的代码增强了我的代码,现在它几乎可以工作了。非常感谢 !编辑:现在它终于开始工作了!感谢您的帮助!
猜你喜欢
  • 2014-03-19
  • 2023-03-06
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多