Draw a border around an undecorated JFrame in Java

Java 8Object Oriented ProgrammingProgramming

At first, set an undecorated frame −

setUndecorated(true);

Now draw a border −

getRootPane().setBorder (.createMatteBorder(3, 3, 3, 3, Color.ORANGE));

The following is an example to draw a border around an undecorated JFrame −

Example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingDemo extends JFrame {
   JLabel label = new JLabel("Welcome!", JLabel.CENTER);
   public SwingDemo() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(new Dimension(500, 300));
      add(label, BorderLayout.CENTER);
      setUndecorated(true);
      getRootPane().setBorder(
         BorderFactory.createMatteBorder(3, 3, 3, 3, Color.ORANGE));
      setVisible(true);
   }
   public static void main(String[] args) {
      new SwingDemo();
   }
}

Output

raja
Published on 08-May-2019 13:03:40
Advertisements