如何使用流布局删除JPanel之间的填充?

时间:2014-12-07 22:31:15

标签: java swing jpanel padding layout-manager

这是我对java应用程序GUI的一部分,我有一个问题。

enter image description here

这个GUI包含一个蓝色的JPanel(容器),默认的FlowLayout作为LayoutManager,它包含一个包含两个JPanel的Box(为了删除水平间距,或者我可以使用setHgaps为零而不是Box)每个都包含一个JLabel。

这是我创建GUI部分的代码。

  private void setupSouth() {

    final JPanel southPanel = new JPanel();
    southPanel.setBackground(Color.BLUE);

    final JPanel innerPanel1 = new JPanel();
    innerPanel1.setBackground(Color.ORANGE);
    innerPanel1.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    innerPanel1.add(new JLabel("Good"));

    final JPanel innerPanel2 = new JPanel();
    innerPanel2.setBackground(Color.RED);
    innerPanel2.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    innerPanel2.add(new JLabel("Luck!"));

   final Box southBox = new Box(BoxLayout.LINE_AXIS);
  southBox.add(innerPanel1);
  southBox.add(innerPanel2);

    myFrame.add(southPanel, BorderLayout.SOUTH);
}

我的问题是如何摆脱外部JPanel(蓝色)和Box之间的垂直填充?

我知道这是填充因为我在Difference between margin and padding?上读到“填充=元素从文本到边框周围(内部)的空间。”

这不起作用,因为这必须与组件之间的间隙(空间)有关.- How to remove JPanel padding in MigLayout?

我尝试了这个,但它也没有用。 JPanel Padding in Java

1 个答案:

答案 0 :(得分:13)

您可以在FlowLayoutset the gaps,即

FlowLayout layout = (FlowLayout)southPanel.getLayout();
layout.setVgap(0);

默认FlowLayout具有5个单位的水平和垂直间隙。在这种情况下,水平无关紧要,因为BorderLayout水平拉伸面板。

或者使用新的FlowLayout简单初始化面板。这将是相同的结果。

new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

修改

  

“我试过了,没用......”

适合我...

enter image description here enter image description here

设置间隙↑不设置间隙↑

import java.awt.*;
import javax.swing.*;

public class Test {

    public void init() {
        final JPanel southPanel = new JPanel();
        FlowLayout layout = (FlowLayout)southPanel.getLayout();
        layout.setVgap(0);
        southPanel.setBackground(Color.BLUE);

        final JPanel innerPanel1 = new JPanel();
        innerPanel1.setBackground(Color.ORANGE);
        innerPanel1.add(new JLabel("Good"));

        final JPanel innerPanel2 = new JPanel();
        innerPanel2.setBackground(Color.RED);
        innerPanel2.add(new JLabel("Luck!"));

        final Box southBox = new Box(BoxLayout.LINE_AXIS);
        southBox.add(innerPanel1);
        southBox.add(innerPanel2);

        southPanel.add(southBox);   // <=== You're also missing this

        JFrame myFrame = new JFrame();
        JPanel center = new JPanel();
        center.setBackground(Color.yellow);
        myFrame.add(center);
        myFrame.add(southPanel, BorderLayout.SOUTH);
        myFrame.setSize(150, 100);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationByPlatform(true);
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Test().init();
            }
        });
    }
}

注意:总是发布一个可运行的示例(正如我所做的那样)以获得更好的帮助。你说它不起作用,但它总是适用于我,所以如果没有一些代码可以运行并证明问题,我们怎么知道你做错了什么?