如何在 Java Swing 中创建圆形标题边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3056089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:08:05  来源:igfitidea点击:

How to create a rounded title border in Java Swing

javaswing

提问by Stephane Grenier

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)

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 类的情况下做到这一点?如果没有,您将如何编写自定义标题边框?我可以画一个带圆角的矩形,但你怎么做才能让标签也有空间?

回答by EMurnane

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:

可以在不实现您自己的 Border 类的情况下创建带有圆角边缘的标题边框。只需将圆形边框传递给 TitledBorder 的构造函数。请尝试以下操作:

LineBorder roundedLineBorder = new LineBorder(Color.black, 5, true);
TitledBorder roundedTitledBorder = new TitledBorder(roundedLineBorder, "Title");

回答by Larzan

Although this thread is a bit old already, maybe someone who stumbles over it might find the solution useful:

虽然这个线程已经有点老了,但也许偶然发现它的人可能会发现解决方案很有用:

You can add a title to any border you want:

您可以为您想要的任何边框添加标题:

  1. implement your custom border class public class MyBorder extends AbstractBorder {...and in the public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)method you can paint your own custom border on the Graphics context

  2. create an instance of this custom border

    Border myborder = new MyBorder();
    
  3. create the TitledBorder using your custom border as a template and add it to the object you want (in this case a JPanel:

    jPanel1.setBorder(BorderFactory.createTitledBorder(myborder , "Border title"));
    
  1. 实现您的自定义边框类,public class MyBorder extends AbstractBorder {...并在该public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)方法中您可以在 Graphics 上下文中绘制您自己的自定义边框

  2. 创建此自定义边框的实例

    Border myborder = new MyBorder();
    
  3. 使用您的自定义边框作为模板创建 TitledBorder 并将其添加到您想要的对象(在本例中为 JPanel:

    jPanel1.setBorder(BorderFactory.createTitledBorder(myborder , "Border title"));
    

You should now see your custom Border and above that the Title with the default settings of the Look&Feel you are using.

您现在应该看到您的自定义边框和上面的标题以及您正在使用的外观和感觉的默认设置。