首页 > 解决方案 > 在 JLabel 的开头和图标之间设置一个间隙

问题描述

我有一个像这样的 Java Swing 表单和 JLabel:

在此处输入图像描述

我需要做的是在 JLabel 的开头插入一个空白:

在此处输入图像描述

所以它不会粘在边界线上。

注1:我已经使用过jLabName.setIconTextGap(35);,但它做了以下事情:

在此处输入图像描述

我需要在图标之前而不是之后插入间隙!

注2:边框设置和类型等设置:

在此处输入图像描述

标签: javaswingjbutton

解决方案


您可以为此使用复合边框

例如。

//get border of your component which is button as you say
Border border = myButton.getBorder();

//create a new empty border with name it margin
Border margin = new EmptyBorder(0,10,0,0); //top 0, left 10 , bottom 0, right 0

//now set compound border to your button(component), with margin
myButton.setBorder(new CompoundBorder(border, margin));

//NOTE: CompoundBorder accepts two borders as arguments, first one is inner border and last one is outer border

推荐阅读