为Graphics2D绘图创建空间 [英] Creating a space for Graphics2D drawings

查看:138
本文介绍了为Graphics2D绘图创建空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个由Graphics2D矩形组成的简单板,但我也希望在这个板下有一个JButton。我知道这个板的确切尺寸(以像素为单位),我试图处理 getContentPane()方法和BoxLayout,如下所示:

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

但RigidArea并非真正隐形,它会覆盖我的绘图。能否请您给我一些如何使其正常工作的提示? :(我只想要一个小按钮,它让我现在坐在这里大约2个小时...

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...

谢谢!

推荐答案


我想绘制一个由Graphics2D矩形组成的简单板

I want to draw a simple board made of Graphics2D rectangles

当你进行自定义绘制时,你还需要覆盖组件的 getPreferredSize(...)方法,以返回组件的大小。

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.

当您添加框架的组件你可以使用默认的BorderLayout:

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

我建议你阅读 Swing教程自定义绘画部分使用布局管理器将提供更多细节和示例。

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

这篇关于为Graphics2D绘图创建空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆