JLabel 的 setBorder 方法导致绘画问题
我有一个扩展 JLabel 的自定义类。 对于该类的特定实例,我想在左侧的文本中添加一些间距。 我需要间距,因为我正在设置此 JLabel 的背景,并且我不希望文本在彩色背景边缘旁边凸起。 我摸索了很多并实现了这个(在绘制函数内):
if (condition) {
bgColor = Color.red;
setBackground(bgColor);
setOpaque(true);
// This line merely adds some padding on the left
setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
}
else {
setOpaque(false);
}
这似乎有效,因为它增加了我想要的间距,但是它有一个不幸的副作用,因为它似乎破坏了整个其余部分的重新绘制。应用程序...似乎只有该特定组件正在重新绘制,而不是应用程序的其余部分。 我最终将其具体追踪到 setBorder 调用...设置任何类型的边框似乎都会导致相同的损坏行为。 我们的应用程序有两个不同的版本,一种在 Java 1.5 中运行,一种在 Java 1.6 中运行,Java 1.6 版本似乎可以正常工作,而 Java 1.5 版本则不能。 无法将旧版本升级到 Java 1.6...我需要一些可以在 Java 1.5 中运行的东西。 另外,我尝试了这个(只是为了看看它是什么样子):
setHorizontalTextPosition(JLabel.CENTER);
而且这似乎也以完全相同的方式破坏了重新绘制。 我查看了我们应用程序的源代码,发现了我们设置边框的其他地方(包括空边框),但在 JLabels 上找不到任何设置(只有面板、按钮等)。 有人以前见过类似的东西吗? 知道如何解决吗? 或者也许有另一种方法来获得我需要的间距,可以解决该错误? 谢谢。
I have a custom class that extends JLabel. For specific instances of that class, I want to add some spacing to the text on the left side. I need the spacing as I'm setting the background of this JLabel and I don't want the text to bump up right next to the edge of the colored background. I fished around quite a bit and implemented this (inside the paint function):
if (condition) {
bgColor = Color.red;
setBackground(bgColor);
setOpaque(true);
// This line merely adds some padding on the left
setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
}
else {
setOpaque(false);
}
This appears to work in that it adds the spacing I want, however it has an unfortunate side effect in that it appears to break the repainting of the whole rest of the application...it appears that only that particular component is repainting and not the rest of the application. I eventually tracked it down to the setBorder call specifically...setting ANY kind of border appears to cause the same broken behavior. We have two different versions of our application, one that runs in Java 1.5 and one that runs in Java 1.6, the Java 1.6 version appears to work correctly while the Java 1.5 version doesn't. It is not possible to upgrade the older version to Java 1.6...I need something that will work in Java 1.5. Also, I tried this (just to see what it looked like):
setHorizontalTextPosition(JLabel.CENTER);
And that also appears to break the repainting in exactly the same way. I looked through the source of our application and found other places where we set borders (including empty borders), but couldn't find any on JLabels (only panels, buttons, etc). Anybody see anything like this before? Know how to fix it? Or perhaps another way to obtain the spacing I require that may work around the bug? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在 Paint 方法中调用该代码。 您不应该这样做,因为它会冻结 EDT,并在摆动绘画管道中产生不需要的循环。
您应该将该代码放在构造函数中,并在应用程序生命周期的其他位置更改组件设计状态。
如果您想了解更多有关 Swing 绘画的信息,请阅读 Push-pixels.org 上的“Swing 绘画管道”帖子。
请注意,您可以使用 BorderFactory.createCompoundBorder 来组合任意两个边框。 然后您可以使用emptyBorder 和任何其他设置间距来绘制外边框。
编辑:添加示例。
The problem is that you're calling that code inside the paint method. You should not do that because it will freeze the EDT with unwanted loops in the swing painting pipeline.
You should put that code on the constructor and change the component design state elsewhere on the app life cycle.
If you want to know a little bit more about Swing painting please read the "Swing painting pipeline" post on pushing-pixels.org.
Note that you can use BorderFactory.createCompoundBorder to combine any two borders. Then you can set spacing with the emptyBorder and any other to draw the outer border.
EDIT: Example added.