Java JFrame border bug
Hello! I came up with an idea, hope I’ll be able to do all this ^^
I’m making a game in Java, and since Java 7 I have the following problem:
Whenever I want to pack my JFrame after creating and filling it, it leaves a white “border” on the right and the bottom. The game is 800×600, but Java makes the frames content space a bit wider.
I can workaround this by using a one-time action event with Swings Timer class. But isn’t there a more elegant solution?
private void makeUI() {
ds = new Drawscreen();
window = new JFrame("Goatz! v"+VERSION+" - By AyCe for LD 25");
window.setIconImage(MediaLib.icon);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
content = window.getContentPane();
content.setPreferredSize(new Dimension(800, 600));
window.pack();
content.add(ds);
window.setLocationRelativeTo(null);
window.setVisible(true);
ds.requestFocusInWindow();
// hackfix for border bug
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.pack();
}
});
t.setRepeats(false);
t.start();
}

The bug
Unfortunatly I don’t have a better sollution for that problem other than Display though, sorry.
JFrame frame = new JFrame(“Goat Murderer”);
frame.setResizable(false);
GoatMurderer gm = new GoatMurderer();
frame.add(gm);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@Caironater: I dont want to use extra libs for java, since its so simple to code games in it anyway ^^
@Joeslayer97: Um.. But how to set the correct resizing then?
@MAT4DOR: And GoatMurderer sets the preferredSize ?
@Drabiter: Yes, I did that. But then it bugs sometimes. So my workaround is to call it after 1 sec, and then it always works.