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

Edited 3 Years Ago by kedxu

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);
}

Edited 3 Years Ago by kedxu

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