I have an application I'm making... And it has a canvas in it. The problem is, on the y axis, my canvas is offset by ten pixels downward and I can't figure out why. You can see what I'm talking about in the screenshot. Do you know what's wrong?

Attachments problem.png 31.33 KB

Here's simplified code for the JFrame:

package gui;

import java.awt.*;

import javax.swing.*;

public class BoidFrame extends JFrame {


	JPanel content = new JPanel();  //The panel inside
	public Canvas canvas = new Canvas();

	public BoidFrame(int x, int y, final Insets insets) {

		setContentPane(content);
		setSize(x + (insets.left + insets.right), y + (insets.top + insets.bottom));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		canvas.setSize(x, y);
		content.add(canvas);
	}
}

And here's the simplified main():

package gui;

import java.awt.*;

public class Main {
	static Insets insets;
	static int x = 400;
	static int y = 300;

	public static void main(String[] args) {

		LoadFrame load = new LoadFrame();
		load.setVisible(true);


		insets = load.getInsets();
		BoidFrame frame = new BoidFrame(x, y, insets);
		frame.setFocusableWindowState(false);
		frame.setVisible(true);
		Canvas canvas = frame.canvas;

		load.setVisible(false);
		frame.setFocusableWindowState(true);
		frame.toFront();
	}
}

22 pixels on top, 0 everywhere else. The insets are only used to figure out the size of the JFrame, because when you set the size of the JFrame it includes the insets. This means the insets do not affect the canvas.

It just makes the frame smaller than it should be, with the same problem.

OK, well, thanks for trying anyway. Maybe it's a Mac Java problem? If you post a runnable version that demonstrates the problem I can try it under Win 7 Java 1.6.22 for you if you like.

Er... Here's what it looks like on Windows 7. The size is different. Although in the picture it looks like the canvas offset is gone, whenever I resize it, it re-appears... Darn. I don't like GUIs...

Attachments Untitled.png 25.97 KB

What layout manager are you using? Personally I would always go with GridBagLayout for a window that can be re-sized - that way you have total control over what happens.

This question has already been answered. Start a new discussion instead.