在 JLabel 的开头和图标之间设置一个间隙

Set a gap between the beginning of the JLabel and the icon

我有一个 Java Swing 表单和 JLabel,如下所示:

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

所以不会卡在边界线上

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

我需要在图标之前而不是之后插入空白!

注2:边框设置和类型及其他设置:

使用以下代码修复它:

a1.setBorder(new CompoundBorder(new EtchedBorder(), BorderFactory.createEmptyBorder(1, 8, 1, 1)));

您可以使用 compound borders

例如。

//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