Creating a space for Graphics2D drawings

故事扮演 提交于 2019-12-31 07:33:08

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/23617127/creating-a-space-for-graphics2d-drawings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!