从顶部到底部堆叠摆动元件 [英] Stack swing elements from top to bottom

查看:82
本文介绍了从顶部到底部堆叠摆动元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下图:

我需要开发一个类似于此的swing GUI。我简单地将它们命名为jLabel,但其中有一些图像和jLabel。可见的默认awt背景是JPanel,每个可见的红色背景是一个serperate JPanel。现在我需要它们如上图所示堆叠。我尝试了一些LayoutManagers但它仍无法正常工作。

I need to develop a swing GUI the looks like this. I simply named them jLabel's but there a few images and jLabels in it. The default awt background visible is a JPanel and each red background visible is a serperate JPanel. Now I need them to get stacked as shown above. I tried a number of LayoutManagers and still it doesn't work.

这里重点是红色div的数量不是常数。如果只有一个红色的div,那么它必须显示在顶部,而不是在中心。据我所知, GridBagLayout 应该可以工作,但它可以使用单个红色jpanel。所有布局管理器都将它们居中,但不是从上到下堆叠它们。

The important point here is that the number of red colored divs are not constant. If there is only one red colored div then it must be displayed at the top, not at the center. As far as i know GridBagLayout should work, but it centers the single red colored jpanel available. All the layout managers are centering them but not stacking them from top to bottom.

推荐答案

即使将锚点设置为NORTH,面板也是如此仍然会集中。您可以通过添加虚拟面板来填充剩余空间来解决它。就个人而言,我会远离GridBagLayout。

Even with anchor set to NORTH then the panels will still be centered. You could work around it by adding a dummy panel to fill the remaining space. Personally I'd stay well away from GridBagLayout though.

JFrame frame = new JFrame();
JPanel content = new JPanel();
content.setBorder(BorderFactory.createLineBorder(Color.red));
frame.setContentPane(content);
frame.getContentPane().setLayout(new GridBagLayout());
frame.setSize(400, 300);

for (int i = 0; i < 3; i++) {
    JPanel panel = new JPanel();
    panel.add(new JLabel("label1"));
    panel.add(new JLabel("label2"));
    panel.add(new JLabel("label3"));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    GridBagConstraints con = new GridBagConstraints();
    con.gridy = i;
    con.gridx = 0;
    con.anchor = GridBagConstraints.NORTHWEST;
    con.ipady = 10;
    frame.getContentPane().add(panel, con);
}

// dummy panel to use up the space (force others to top)
frame.getContentPane().add(
        new JPanel(),
        new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.NORTHWEST,
                GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0,
                0));

frame.setVisible(true);

GroupLayout示例(我最喜欢的布局管理器)。

GroupLayout example (my favourite layout manager).

JFrame frame = new JFrame();
JPanel content = new JPanel();
frame.setContentPane(content);
frame.getContentPane().setLayout(
        new BoxLayout(content, BoxLayout.Y_AXIS));
frame.setSize(400, 300);
GroupLayout gLayout = new GroupLayout(content);
content.setLayout(gLayout);
ParallelGroup hGroup = gLayout.createParallelGroup();
gLayout.setHorizontalGroup(hGroup);
SequentialGroup vGroup = gLayout.createSequentialGroup();
gLayout.setVerticalGroup(vGroup);
for (int i = 0; i < 3; i++) {
    JPanel panel = new JPanel();
    panel.add(new JLabel("label1"));
    panel.add(new JLabel("label2"));
    panel.add(new JLabel("label3"));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    hGroup.addComponent(panel);
    vGroup.addComponent(panel, GroupLayout.PREFERRED_SIZE,
            GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
    vGroup.addGap(10);
}

frame.setVisible(true);

这篇关于从顶部到底部堆叠摆动元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆