EmptyBorder到底是做什么的? [英] What exactly does an EmptyBorder do?

查看:115
本文介绍了EmptyBorder到底是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解Java摇摆代码.我看到其中使用EmptyBorder的一段代码,但是我不明白它在做什么.我试图注释该部分并在未应用emptyborder的情况下运行,但对我而言并没有显示任何区别.还是我只是错过了UI的一些细微变化?

I'm trying to understand java swing code. I see a piece of code using EmptyBorder in it, but I do not understand what it is doing. I tried to comment that part and run without emptyborder applied, but it doesn't really show any difference to me. Or am I just missing out some minute change in UI?

代码:

EmptyBorder border1 = new EmptyBorder(3, 0, 6, 550);
.....
JLabel pdt = new JLabel();
pdt.setIcon(icon);
pdt.setText("blah blah");
pdt.setIconTextGap(5);
pdt.setBorder(border1);
....

border1在这里做什么?

What does border1 do here.

我可以使用EmptyBorder在FlowLayout中的一组控件之间提供间距吗?

Can I use EmptyBorder to give spacing between a set of controls in FlowLayout?

推荐答案

正如我在评论中提到的,它只是在其添加的组件周围添加了透明边框,有时效果很难看,具体取决于布局您使用的管理器,因此请在流布局中包含一些正在使用的图片(非常容易看到对流布局的影响):

As i mentioned in my comment, it just adds an transparent border around the components its added to, sometimes the effect can be hard to see, depending on the layout manager you use, so ill include some pictures of it in use on a flow layout (very easy to see the effect on a flow layout):

这是没有添加边框的流布局:

here is the flow layout with no added border:

,这是流布局,其中边框的左侧和右侧分别设置为100和300,并且边框应用于第一个标签.

and here is the flow layout with the left and right of the border set to 100 and 300 respectively, and the border is applied to the first label.

最后这是一些代码供您测试事物如何变化:

and finally here is some code for you to test out how things change:

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class EmptyBorderShowCase extends JFrame{

private static final long serialVersionUID = 1L;

public EmptyBorderShowCase(){
    JPanel displayPanel = new JPanel(new FlowLayout());
    final int BOTTOM = 0;
    final int LEFT = 100;
    final int RIGHT = 300;
    final int TOP = 0;
    EmptyBorder border1 = new EmptyBorder(TOP, LEFT, BOTTOM,RIGHT );

    JLabel firstLabel = new JLabel("FIRST");
    firstLabel.setBorder(border1);

    JLabel secondLabel = new JLabel("SECOND");

    displayPanel.add(firstLabel);
    displayPanel.add(secondLabel);
    setContentPane(displayPanel);

    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String[]args){
    new EmptyBorderShowCase();
}

}

这篇关于EmptyBorder到底是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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