How can I make the inner size 500x500 pixel?
Or should I hardcode the 28px macOS top-bar for windows?
My simple code:
import javax.swing.JFrame;
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
CodePudding user response:
Don't set the JFrame
size. Use a JPanel
and add that to the JFrame and set the size of the JPanel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen.
frame.setVisible(true);
If you are extending JPanel
it is best to set the size by overridding the following:
@Override
public Dimenison getPreferredSize() {
return new Dimension(500,500);
}
It is also considered best practice to do most layouts and especially painting inside JPanel(s)
and not the JFrame
.