我已对 java.awt.Frame< /code>
并覆盖了 paint()
方法,因为我希望手动绘制窗口的全部内容。
但是,在图形对象上,(0,0) 对应于标题栏装饰内的窗口左上角,而不是第一个可绘制像素。
我可以以跨平台的方式确定第一个可绘制像素的坐标(即装饰的高度),避免使用特定于 Mac OS X 的 忽悠因素? 我是否会被迫嵌套 Panel组件以便找到窗口的实际可绘制区域?
在这里,我的代码无法将蓝色方块置于窗口的可绘制区域内居中:
@Override
public void paint (Graphics g) {
g.setColor(Color.BLUE);
g.setPaintMode();
g.fillRect(30, 30, getWidth()-60, getHeight()-60);
}
I have subclassed java.awt.Frame
and have overridden the paint()
method as I wish to draw the entire contents of the window manually.
However, on the graphics object, (0,0) corresponds to the upper left hand corner of the window inside the title bar decoration, not the first drawable pixel.
Can I determine the co-ordinate of the first drawable pixel (ie, the height of the decoration) in a cross-platform manner, avoiding using a Mac OS X-specific fudge factor? Will I be forced to nest a Panel component in order to find the actual drawable area of the window?
Here, my code fails to centre the blue square inside the paintable area of the window:
@Override
public void paint (Graphics g) {
g.setColor(Color.BLUE);
g.setPaintMode();
g.fillRect(30, 30, getWidth()-60, getHeight()-60);
}
发布评论
评论(2)
您可以通过调用 getInsets 方法(在 Container 中定义)来查找帧插图。 框架插入在框架 API 文档的顶部进行了讨论。
You can find the frame insets by calling the getInsets method (defined in Container). Frame insets are discussed at the top of the Frame API docs.
那么您想绘制整个区域并且根本不需要标题栏吗?
假设您使用 JDk 1.4(至少),那么您可以将框架声明为“未装饰”(java.awt.Frame#setUndecorated(boolean))。 这样就不会创建标题栏,因此框架可绘制区域与框架消耗区域相同。
So you want to paint the whole area and don't want a title bar at all?
Assuming that you use JDk 1.4 (at least) then you can declare the frame to be "undecorated" (java.awt.Frame#setUndecorated(boolean)). This way no title bar is created and therefore the frames-paintable area is the same as the frames-consumed area.