How to create a rounded title border in Java Swing

后端 未结 2 1191
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 05:16

I do understand that to create a title border, you do something like:

BorderFactory.createTitledBorder(\"  Your Title  \");

However this cr

相关标签:
2条回答
  • 2021-01-06 05:56

    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");
    
    0 讨论(0)
  • 2021-01-06 05:57

    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"));
      

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

    0 讨论(0)
提交回复
热议问题