问题描述
我知道要创建标题边框,您可以执行以下操作:
I do understand that to create a title border, you do something like:
BorderFactory.createTitledBorder(" Your Title ");
然而,这会创建一个矩形边框,而我需要一个带有弯角的矩形。
However this creates a rectangle border whereas I need a rectangle with curved corners.
根据我的理解,您可以通过以下方式创建自己的自定义边框:
Now from what I understand you can create your own custom border by:
class CustomBorder implements Border
{
...
}
问题是我不知道如何编写覆盖该方法的代码:
The problem is that I'm not sure how to write the code that overrides the method:
public void paintBorder(Component component, Graphics g, int x, int y, int width, int height)
或更好,有没有办法在不实现自己的Border类的情况下完成它?如果没有,你会如何编写自定义标题边框?我可以画一个带圆角的矩形,但你怎么做才能有标签的空间呢?
Or better yet, is there a way to do it without implementing your own Border class? And if not, how would you write that custom Title Border? I'm ok with drawing a rectangle with rounded corners, but how do you do it so that there's space for the label too?
推荐答案
可以创建带有圆边的标题边框,而无需实现自己的Border类。只需将圆形边框传递给TitledBorder的构造函数即可。请尝试以下操作:
It is possible to create a title border with rounded edges without implementing your own Border class. Simply pass a rounded border to TitledBorder's constructor. Try the following:
LineBorder roundedLineBorder = new LineBorder(Color.black, 5, true);
TitledBorder roundedTitledBorder = new TitledBorder(roundedLineBorder, "Title");
这篇关于如何在Java Swing中创建圆角标题边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!