java 如何在仍然使用流布局的 JPanel 中删除填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27348665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:40:15  来源:igfitidea点击:

How to remove the padding between in the JPanel still using a flow layout?

javaswingjpanelpaddinglayout-manager

提问by committedandroider

Here's the portion of my java application GUI that I have a question about.

这是我有疑问的 Java 应用程序 GUI 部分。

enter image description here

在此处输入图片说明

What this GUI consists is a blue JPanel(container) with default FlowLayout as LayoutManager that contains a Box which contains two JPanels(to remove the horizontal spacing or i could have used setHgaps to zero for that matter instead of a Box) that each contains a JLabel.

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

Here's my code for creating that part of the GUI.

这是我创建 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);
}

My question is how would i get rid the vertical padding between the outer JPanel(the blue one) and the Box?

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

I know this is padding because i read on Difference between margin and padding?that "padding = space around (inside) the element from text to border."

我知道这是填充,因为我阅读了边距和填充之间的差异?“padding = 元素周围(内部)从文本到边框的空间。”

This wouldn't work because this has to due with gaps(space) between components.- How to remove JPanel padding in MigLayout?

这是行不通的,因为这必须与组件之间的间隙(空间)有关。- 如何删除 MigLayout 中的 JPanel 填充?

I tried this but it didn't work either. JPanel Padding in Java

我试过这个,但它也不起作用。Java 中的 JPanel 填充

回答by Paul Samsotha

You can just set the gapsin the FlowLayout, i.e.

你可以只设置空白FlowLayout,即

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

The default FlowLayouthas a 5-unit horizontal and vertical gap. Horizontal doesn't matter in this case as the BorderLayoutis stretching the panel horizontally.

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

Or simple initialize the panel with a new FlowLayout. It'll be the same result.

或者简单地使用新的FlowLayout. 这将是相同的结果。

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

Edit:

编辑:

"I tried that, didn't work.."

“我试过了,没用……”

Works for me...

对我有用...

enter image description here    enter image description here

在此处输入图片说明    在此处输入图片说明

     Setting the gap ↑              Not setting the gap ↑

     设置间隙 ↑ 不设置间隙 ↑

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

Note: Always post a runnable example (as I have done) for better help. You say it doesn't work, but it always works for me, so how would we know what you're doing wrong without some code that will run and demonstrate the problem?

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