2
public class n00767255 {
    public static void main(String[] args) {
        CarFrame frame = new CarFrame();
        frame.setSize(600,480);
        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

        while(true)
        {
            frame.repaint();
        }   
    }
}

class CarFrame extends JFrame {
    CarFrame() {
        setLayout(new GridLayout(3,1));

        final CarPanel car1 = new CarPanel();
        car1.initCar(10,50,2,150,70,40);
        add(car1);

        final CarPanel car2 = new CarPanel();
        car2.initCar(10,50,2,150,70,40);
        add(car2);

        JButton startCar1 = new JButton("Start Car 1");

        JPanel panel = new JPanel();
        panel.setSize(600,40);
        panel.add(startCar1);

        add(panel);

        startCar1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!car1.getMovingF())
                {
                    car1.modifyMovingF();
                }
            }
        });
    }
}

What I want this to do is to create the two CarPanels with the basic car shape and for those both to by 600X200 and be spaced properly so the full objects can be seen. Under that I want to paste the panel with a bunch of different buttons that do different things. Here my problem is that the cars never have enough space to be seen entirely, and using different layout managers hasn't helped.

EDIT

After numerous changed this code is not working at all the way I would expect. At this point the first car is the only one shown and the start car button is at the top of the frame.

MarthyM
  • 1,839
  • 2
  • 21
  • 23
Ty_
  • 828
  • 1
  • 11
  • 21
  • *"code is not working at all the way I would expect."* It has so many problems like `while(true) { frame.repaint();` that I don't quite know where to start. I recommend you toss the code out and go back to the relevant sections of the Java Tutorial with attention to The Swing Trail and particularly 'laying out components in a container' and 'custom painting'. – Andrew Thompson Nov 14 '12 at 09:49

3 Answers3

2

Have a look at frame.pack: http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack%28%29 - It sets the size of a window so that it fits the preferred size of its contents.

If the content of the frame has a correclty set preferred size, just call

frame.pack();

instead of

frame.setSize(600,480);

in your application - that should do the trick.

Edit: Another problem with your code is that you probably use the wrong layout manager. GridLayout creates a regular grid, this means all cells have the same size. You should try a layout that supports a more flexible sizing of cells, like JGoodies FormLayout or MigLayout.

I have tried your code with the following CarPanel implementation, GridLayout seems to ignore the sizes of it's children:

private class CarPanel extends JPanel {
    public CarPanel() {
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 20);
    }
}
David Tanzer
  • 2,732
  • 18
  • 30
  • If I use frame.pack(); then all it shows is the start car 1 button. – Ty_ Nov 13 '12 at 10:17
  • Does your "CarPanel" class has a correct preferred size? That is, does the class return the correct dimensions of the component when you call getPreferredSize()? Otherwise override "getPreferredSize"... – David Tanzer Nov 13 '12 at 10:19
  • This is my CarPanel constructor. CarPanel() { setSize(600,200); setMinimumSize(new Dimension(600,200)); } – Ty_ Nov 13 '12 at 10:23
  • You are probably also using an ill-suited layout manager, see my edit of the answer for details. – David Tanzer Nov 13 '12 at 11:01
1

Try using JFrame's pack method.

public static void main(String[] args) {
    CarFrame frame = new CarFrame();
    frame.setPreferredSize(new Dimension(600,480));
    frame.setLocationRelativeTo(null);

    frame.pack();
    //...rest of code
  }
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • If I use frame.pack(); then all it shows is the start car 1 button. – Ty_ Nov 13 '12 at 10:16
  • Changing this line of code caused the window to be extremely small. Literally just the border was showing and the window had to be expanded to see any of the objects. – Ty_ Nov 13 '12 at 10:25
-1

See the API for methods like setSize setMinimumSize setMaximumSize and setPreferredSize

gh.
  • 339
  • 1
  • 3
  • 20