Question

I want to draw a simple board made of Graphics2D rectangles but I also want to have one JButton under this board. I know the exact dimensions of this board in pixels and I was trying to deal with getContentPane() method and BoxLayout, like this:

frame.getContentPane().add(board);
frame.getContentPane().add(Box.createRigidArea(new Dimension(bWidth, bHeight)));
frame.getContentPane().add(new JButton("Start"));
frame.pack();

But RigidArea isn't truly invisible and it overrides my drawings. Could you please give me some tips how to make it work properly? :( I wanted just one little button and it made me sit here for around 2 hours now...

Thanks!

Was it helpful?

Solution

I want to draw a simple board made of Graphics2D rectangles

When you do custom painting you also need to override the getPreferredSize(...) method of your component to return the size of the comoponent.

Then the layout manager can use this information and you will not need to use the rigid area.

When you add the components to the frame you can just use the default BorderLayout:

frame.add(board, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);

I suggest you read the Swing tutorial. There are section on custom painting and using layout managers that will provide more detail and examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top