Have borders on a JWindow/JFrameTag(s): Swing


You can't have a border directly on a JWindow or JFrame. You need to put a JPanel with the desired border using the default JWindow/JFrame LayouManager (a BorderLayout). Then the JPanel is extended giving the impression that its borders are the JWindow one.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;

public class MyWindow{
  public static void main(String s[]) {
    JWindow win = new JWindow();
    
    JPanel pan = new JPanel();
    pan.setBorder(new LineBorder(Color.blue));
    win.getContentPane().add(pan,"Center");
    
    pan.setLayout(new FlowLayout());
    pan.add(new JButton("Hello"));
    pan.add(new JButton("World"));

    win.setSize(200,200);
    win.setVisible(true);
    }
}

blog comments powered by Disqus