简体   繁体   English

Java Swing边框没有厚度

[英]Java Swing border with no thickness

I have a bunch of JLabels in a GridBagLayout . 我在GridBagLayout有一堆JLabels I would like to highlight which one is "selected" by adding a line border around the "selected one". 我想通过在“选定的一个”周围添加线条边框来突出显示“选定的”一个。 The problem with this is that the thickness of the border separates the JLabels apart and forms gaps in between them all. 问题在于边框的厚度将JLabels分开并在它们之间形成间隙。 Is it possible to make the border with 0 thickness but still be visible? 是否可以使边框的厚度为0但仍然可见?

Is it possible to make the border with 0 thickness but still be visible? 是否可以使边框的厚度为0但仍然可见?

You can use a CompoundBorder . 您可以使用CompoundBorder Create a LineBorder as the outer and an EmptyBorder (with -1 offsets) as the inner. 创建一个LineBorder作为外和EmptyBorder (带-1偏移)作为内。

The total size of the Border will be 0 and not take up any space, but it will paint on top of the label, instead of outside the bounds of the label. Border的总大小将为0,不占用任何空间,但是它将在标签的顶部而不是在标签的边界之外绘制。

Instead, set each "non-selected" JLabel with an EmptyBorder of the same pixel thickness as the LineBorder . 而是使用与EmptyBorder相同的像素厚度的LineBorder设置每个“未选择的” JLabel

When selected, change the Border to a LineBorder and when unselected, change it to an EmptyBorder . 选中后,将Border更改为LineBorderLineBorder选中时,将其更改为EmptyBorder

This will stop the layout from changing size...as it hasn't. 这将阻止布局更改大小,因为它没有更改。

Also, a Border of 0 pixel thickness is...invisible... 此外, 0像素厚度的Border是...不可见的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM