如何使用标准Java布局管理器纠正/居中GridLayout? [英] How to correct/center GridLayout using standard Java layout managers?

查看:96
本文介绍了如何使用标准Java布局管理器纠正/居中GridLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码代表问题。由于我有南北面板的高度设置,其余部分使用GridLayout进入中心面板。我认为,因为它不能在它的行之间平等地共享剩余的像素,所以它只留下它们。因此,在下面的代码中,我们在南面板上有一条丑陋的白线。

The below code represents the problem. Since I have heights of the north and south panels set the rest of it goes to the center panel using GridLayout. I think that since it cannot share the leftover pixels equally among its rows it just leaves them. Therefore in the below code we have ugly white line over south panel.

我的问题是:如何确保当GridLayout是不占用整个空间它至少居中?

My question here is: How to make sure that when the GridLayout is not taking the whole space it is at least centered?

通常我会使用TableLayout并对情况进行排序,但由于我写的答案我只想使用标准管理器。事先知道这对我来说非常有用。

Normally I would use TableLayout and situation is sorted, but since I was writing an answer I wanted to use only standard managers. Knowing this would be very useful for me thanks in advance.

示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AligningButonsTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {                   
                JFrame f = new JFrame();
                f.setSize(800, 600);
                double CONSTANT_FACTOR = .1;
                int noOfRows = 5;
                JPanel centerP = new JPanel(new GridLayout(noOfRows,1));

                for(int i = 0; i < noOfRows; i++)
                {   
                    BoxPanel bP = new BoxPanel();
                    centerP.add(bP);
                }
                JPanel contentPane = new JPanel(new BorderLayout());                
                f.setContentPane(contentPane);
                contentPane.add(centerP, BorderLayout.CENTER);
                JPanel southP = new JPanel();
                southP.setBackground(Color.RED.darker());//southP.setOpaque(false);
                southP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
                contentPane.add(southP, BorderLayout.SOUTH);
                JPanel northP = new JPanel();
                northP.setBackground(Color.RED.darker());//northP.setOpaque(false);
                northP.setPreferredSize(new Dimension(1, (int)(CONSTANT_FACTOR* f.getHeight())));
                contentPane.add(northP, BorderLayout.NORTH);            
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }   
}

class BoxPanel extends JPanel
{
    public BoxPanel()
    {
        setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.RED));
        setBackground(Color.DARK_GRAY);
    }   
}


推荐答案

< a href =http://download.oracle.com/javase/6/docs/api/javax/swing/BoxLayout.html\"rel =nofollow> BoxLayout 使用 Box.createVerticalGlue()在组件之间分配空间方面做得非常好。此示例使用 Box.createVerticalStrut(),top和bottom。间隔器在 如何使用BoxLayout:使用Invisible中描述作为填充程序的组件

BoxLayout does a pretty good job of distributing the space between components using Box.createVerticalGlue(). This example uses Box.createVerticalStrut(), top and bottom. The spacers are described in How to Use BoxLayout: Using Invisible Components as Filler.

附录: BoxTest2 是使用 BoxLayout 创建固定尺寸的边缘面板和垂直胶水,以更均匀地分布空间。 Box.Filler 也可用于控制剩余垂直空间。

Addendum: BoxTest2 is a variation that uses BoxLayout to create fixed-size edge panels and vertical glue to distribute the space more evenly. Box.Filler may also be used to control the "leftover" vertical space.

/** @see http://stackoverflow.com/questions/6072956 */
public class BoxTest2 {

    private static final int WIDE = 480;
    private static final int HIGH = WIDE / 8;
    private static final int ROWS = 5;
    private static final Box center = new Box(BoxLayout.Y_AXIS);

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

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                center.setOpaque(true);
                center.setBackground(Color.lightGray);
                center.add(Box.createVerticalGlue());
                center.add(new EdgePanel());
                for (int i = 0; i < ROWS; i++) {
                    center.add(new BoxPanel());
                }
                center.add(new EdgePanel());
                center.add(Box.createVerticalGlue());
                f.add(center, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class EdgePanel extends JPanel {

        public EdgePanel() {
            Dimension d = new Dimension(WIDE, 2 * HIGH / 3);
            setPreferredSize(d);
            setBackground(Color.red.darker());
        }
    }

    private static class BoxPanel extends JPanel {

        public BoxPanel() {
            setPreferredSize(new Dimension(WIDE, HIGH));
            setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.red));
            setBackground(Color.darkGray);
        }
    }
}

这篇关于如何使用标准Java布局管理器纠正/居中GridLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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