如何使用流布局删除JPanel之间的填充? [英] How to remove the padding between in the JPanel still using a flow layout?

查看:146
本文介绍了如何使用流布局删除JPanel之间的填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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部分的代码。

Here's my code for creating that part of the 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之间的垂直填充(蓝色的一个)和盒子?

My question is how would i get rid the vertical padding between the outer JPanel(the blue one) and the 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."

这不起作用,因为这必须与组件之间的间隙(空间)有关.- 如何删除JPanel填充在MigLayout?

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

我试过这个,但它也没有用。 Java中的JPanel填充

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

推荐答案

你可以 FlowLayout 中设置差距,即

You can just set the gaps in the FlowLayout, i.e.

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

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

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

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

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

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

修改:


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

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

为我工作.. 。

    

   

     ; 设定差距↑             没有设置差距↑

     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?

这篇关于如何使用流布局删除JPanel之间的填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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