Java Swing How to - Make a JFrame a certain size, not including the border








Question

We would like to know how to make a JFrame a certain size, not including the border.

Answer

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
/*w  ww .  j a v  a 2s . co m*/
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame("Fixed size content");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Container c = f.getContentPane();
    c.setBackground(Color.YELLOW);

    Dimension d = new Dimension(400, 40);
    c.setPreferredSize(d);
    f.pack();
    f.setResizable(false);
    f.setVisible(true);
  }
}