简体   繁体   中英

Java swing background color fill outside the border

I'm building a Java Swing app and I'm trying to achieve a backgroundcolor inside the border,

for some reason it spills outside the border as you can see here:

在此处输入图像描述

Fyi I need the TitleBorder to remain -> "TitledBorder.TOP" and not "BELOW_TOP"

Build with IntelliJ IDEA with Java SDK 8

Thank you for your time

for some reason it spills outside the border

That is the way Swing painting works.

All Swing components have a parent / child relationship. So the top level component is painted, then the child is painted on top of the parent etc all the way down the parent/child tree. So each child overrides the background of its parent.

Read the section from the Swing tutorial on A Closer Look at the Painting Mechanism

  1. So first the background of the panel is painted.
  2. then the border is painted on top of the panel. In the case of a TitleBorder only the text and line is painted on top of the background.

If this is not acceptable to you then you would need to create a custom Border that would first:

  1. paint the outer area of the Border in the same color as the background of the parent component.

  2. Then draw the TitleBorder .

Or, maybe you could somehow do this by using a CompoundBorder .

Read the section from the Swing tutorial on How to Use Borders for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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