java - Java swing背景色填充边框外
问题描述
我正在构建一个 Java Swing 应用程序,我正在尝试在边框内实现背景色,
出于某种原因,它会溢出到边界之外,如您在此处看到的:
仅供参考,我需要保留 TitleBorder ->“TitledBorder.TOP”而不是“BELOW_TOP”
使用带有 Java SDK 8 的 IntelliJ IDEA 构建
感谢您的时间
解决方案
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
- So first the background of the panel is painted.
- 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:
paint the outer area of the Border in the same color as the background of the parent component.
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.
推荐阅读
- django - 如何使用 nginx 反向代理设置 Django
- c# - 方法中不接受返回语句
- c# - 执行 lambda 表达式时,Entity Framework 抛出错误
- c# - 谁能帮我完成这项任务或解释如何做?
- c - 使用制表符进行 C 输出格式化
- entity-framework - 如何使用温莎城堡 4.1.1。在我的 ASP.NET Core MVC 项目中为我的控制器提供依赖项?
- python - 压缩两个列表,一个包含相似的元素
- opencart2.x - 当 ocmod 扩展安装 vqmod 不起作用
- video - 使用 Google Chrome 开发工具下载/查找和流式传输 Youtube 视频
- python - 如何在 Python 中更改 Firefox webdriver 的用户代理?