【问题标题】:How to make BoxLayout vertical but children flow top-aligned?如何使 BoxLayout 垂直但子项顶部对齐?
【发布时间】:2012-06-21 23:11:54
【问题描述】:

我正在尝试使用垂直、顶部对齐的布局。这就是我所拥有的:

JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

MyImagePanel panelImage = new MyImagePanel();
panelImage.setSize(400, 400);

pane.add(new JComboBox());
pane.add(panelImage);
pane.add(new JButton("1"));
pane.add(new JButton("2"));
pane.add(new JButton("3"));

JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.add(pane);
frame.pack();
frame.setVisible(true);

所有控件都出现了,但看起来在运行时在它们的顶部和底部之间应用了填充,因此它们有点垂直居中。这就是我想要的:

-----------------------------------------------------
| ComboBox |                                        |
------------                                        |
|          |                                        |
| Image    |                                        |
|          |                                        |
------------                                        |
| Button 1 | Any additional space fills the right   |
------------                                        |
| Button 2 |                                        |
------------                                        |
| Button 3 |                                        |
------------                                        |
|                                                   |
|  Any additional space fills the bottom            |
|                                                   |
-----------------------------------------------------

如何让 BoxLayout 做到这一点?

谢谢

-------------- 更新 --------- ----

能够使用这个:

Box vertical = Box.createVerticalBox();
frame.add(vertical, BorderLayout.NORTH);
vertical.add(new JComboBox());
vertical.add(new JButton("1"));  
...

得到我想要的。

【问题讨论】:

  • “任何额外的空间填充底部”是什么意思你的意思是组件应该扩展以填充空白空间,或者什么都不应该出现并且所有组件都应该保持它们的preferredSize?
  • 是的,组件将保持其首选大小 - 如果父窗口恰好高于所有组件的组合高度,则只会填充空白空间。
  • 不要用答案更新问题。发布答案作为答案,并接受它。 ;-)

标签: swing


【解决方案1】:

更合适的 LayoutManager 是 GridBagLayout,但还有其他选择:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test3 {

    protected static void initUI() {
        JPanel pane = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        JPanel panelImage = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.fillRect(0, 0, 400, 400);
            }
        };
        panelImage.setPreferredSize(new Dimension(400, 400));

        pane.add(new JComboBox(), gbc);
        pane.add(panelImage, gbc);
        pane.add(new JButton("1"), gbc);
        pane.add(new JButton("2"), gbc);
        // We give all the extra horizontal and vertical space to the last component
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        pane.add(new JButton("3"), gbc);

        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 2015-10-18
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多