Java Swing Tutorial - Java BorderFactory .createTitledBorder (Border border, String title, int titleJustification, int titlePosition, Font titleFont)








Syntax

BorderFactory.createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont) has the following syntax.

public static TitledBorder createTitledBorder(Border border,    String title,    int titleJustification,    int titlePosition,    Font titleFont)

Example

In the following code shows how to use BorderFactory.createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont) method.

/*from w w  w .  ja  v  a 2 s  .  com*/

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class Main extends JFrame {
  public Main() {
    getContentPane().setLayout(new FlowLayout());
    JLabel labelTwo = new JLabel("www.java2s.com");
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    
    add(labelTwo);
    
    
    JLabel labelFour = new JLabel("TitledBorder");
    labelFour.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(10, 10,
        10, 10, Color.pink), 
        "Title",
        TitledBorder.ABOVE_BOTTOM,
        TitledBorder.BOTTOM,
        new Font("font name",Font.BOLD,19),Color.BLACK));
    add(labelFour);
    
  }

  public static void main(String[] args) {
    JFrame frame = new Main();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

  }
}