Change TitledBorder Direction in Java

Description

The following code shows how to change TitiledBorder Direction.

Example


 /*ww  w. java 2 s  . c  o m*/
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Border lineBorder = new LineBorder(Color.RED, 5);
    
    Font font = new Font("Serif", Font.ITALIC, 12);
    Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT,
        TitledBorder.BELOW_BOTTOM, font, Color.RED);

    
    JLabel aLabel = new JLabel("Bevel");
    aLabel.setBorder(titledBorder);
    aLabel.setHorizontalAlignment(JLabel.CENTER);

    frame.add(aLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Change TitledBorder Direction in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer