The setSize() method of a Frame only resizes the frame itself, not the canvas inside. Is there a way to resize a frame based on what size you want the interior to be?

I made this image to explain what I'm talking about: I used setSize(500,500);
http://oi44.tinypic.com/4uymur.jpg

I suppose I could count pixels, but that stuff is platform-dependent, and plus it's not elegant :P

Recommended Answers

All 5 Replies

i usualy set the prefered size of the canvas to the size desired, then call up the pack() method.

Unfortuately that doesn't work either :(

It depends on yout layout manager. You may need to call SetPreferredSize, setMinimumSize, setMaximumSize and/or setSize before calling pack. But is does work.

Is this the right way to approach it?

void createFrame(int[] location, int[] size){
        Frame f = new Frame();
        f.setLocation(location[0],location[1]);
        f.setPreferredSize(new Dimension(size[0],size[1]));
        f.setMinimumSize(new Dimension(size[0],size[1]));
        f.setMaximumSize(new Dimension(size[0],size[1]));
        f.setSize(size[0],size[1]);
        f.pack();
        f.setVisible(true);
}

Nevermind, found a way that works using your advice. Thanks for the help, guys!

Solution:

void createFrame(int[] location, int[] size) {
        JFrame f = new JFrame();
        f.setLocation(location[0],location[1]);
        f.getContentPane().setPreferredSize(new Dimension(size[0],size[1]));
        f.pack();
        f.setVisible(true);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.