resizing a Jframe

I'm trying to resize the size of my Jframe to fit the contents but it's not working. The setSize command has no effect on my window size (always very small)

privatestaticvoid createAndShowGUI()

{

JFrame frame =new JFrame("GameDisplay");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new GameDisplay());

frame.setSize(800,600);

frame.pack();

frame.setVisible(true);

}

[743 byte] By [JFactor2004a] at [2007-11-27 10:51:38]
# 1

Try removing frame.pack()

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 2

From the javadoc

public void pack()

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size.

The Window will be validated after the preferredSize is calculated.

you are setting the size, then resetting the size again.

SomeoneElsea at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 3

That worked. What does the pack() do?

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 4

Why do you call a method that you don't know what does?

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 5

Because I thought you needed it to get JFrames to work

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 6

> Because I thought you needed it to get JFrames to work

When you make an assumtion you make an ss out of u and mtion.

Then why not read up about it and make sure?

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 7

Now that the frame is the right size I need to but the buttons in the correct locations but b/c of how my code is set up it won't let me do anything. Because I have a static method, I'm not allowed to touch reset. If I put the creation of the button into the static method though, I'm not allowed to use addActionListener(this)

public class GameDisplay extends JPanel implements MouseListener, ActionListener

{

private GamePlay game;

public static Image[] images;

private JButton reset;

public GameDisplay()

{

super();

//Load Images

images = loadImages();

//Start Game

game = new GamePlay();

//Add Components

reset = new JButton("Reset");

reset.addActionListener(this);

addMouseListener(this);

add(reset);

}

private static void createAndShowGUI()

{

JFrame frame = new JFrame("GameDisplay");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new GameDisplay());

frame.setSize(800,700);

reset.setBounds(200,200,200,200);//Won't let me do this

//frame.pack();

frame.setVisible(true);

}

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 8

I did read up on it, I looked at example source code and stuff and everything had frame.pack(). I didn't realize it just resized I thought it actually "packed" stuff together that I needed.

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 9

Movereset.setBounds(200,200,200,200);

into the GameDisplay constructor.

Why aren't you using layout managers?

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 10

I don't really understand what they are. I've seen that site but I'm not understanding

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 11

I moved the button code into the GameDisplay constructor and it compiles and runs but the button doesn't go anywhere, it stays where it was

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 12

In GameDisplay constructor you call super() which invokes JPanel's default constructor which initializes the panel to use FlowLayout as it's layout manager.

If you want to use absolute positioning you can try calling super(null) instead, effectively using no layout manager. This however forces you to position all components on that panel absolutely.

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 13

That worked. So I was using a layout manager then? The default one. If I'm not allowed to put a button where I want it, whats the point of a layout manager?

JFactor2004a at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 14

Generally speaking, absolute positioning is considered bad practice.

Layout managers help you layout components in some specific way, e.g. FlowLayout lays out components flowing from left to right (the default) or from right to left. This layout is ideal to use for instance in toolbars.

dwga at 2007-7-29 11:32:34 > top of Java-index,Java Essentials,New To Java...
# 15

That makes sense, but since I'm using an image, and I need the buttons to not be on top of the image, I guess in this case its ok to do it?

JFactor2004a at 2007-7-29 11:32:39 > top of Java-index,Java Essentials,New To Java...