How to Change the Border of a JFrame in Java
In this tutorial, we are going to see how to change the border of a JFrame in Java. You can draw borders around an undecorated JFrame. Just get the root panel of the JFrame and set its borders with the method setBorder(Border border).
[st_adsense]
First remove the frame decoration:
frame.setUndecorated(true);
Then draw the border:
frame.getRootPane().setBorder( BorderFactory.createMatteBorder(4, 4, 4, 4, Color.BLUE) );
[st_adsense]
Java Program to Change the Border of a JFrame:
import java.awt.*; import javax.swing.*; public class BorderFrameExample extends JFrame { JLabel l = new JLabel("Welcome to StackHowTo!", JLabel.CENTER); public BorderFrameExample() { //add label to frame add(l, BorderLayout.CENTER); //remove the decoration setUndecorated(true); //set border getRootPane().setBorder( BorderFactory.createMatteBorder(4, 4, 4, 4, Color.BLUE) ); setSize(250,250); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new BorderFrameExample(); } }
Output:
[st_adsense]