即使在重新调整大小时,如何获得屏幕的中间位置 [英] How to get the EXACT middle of a screen, even when re-sized

查看:104
本文介绍了即使在重新调整大小时,如何获得屏幕的中间位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这类问题包含2部分。

Ok, this sort of have 2 parts of the question.


  1. 当我制作一个 JFrame ,并在其上绘制一些东西,即使我将宽度设为400,并使其成为当项目击中它时(当然允许物品宽度)它会反弹回来。但出于某种原因,它总是离屏幕大约10个像素。有没有办法解决它,或者我只需要通过加/减数字来补偿?

  1. When I make a JFrame, and draw something on it, even if I make the width 400, and make it so that when an item hits it (Allowing for an items width, of course) It bounces back. But it always goes about 10 pixels off the screen for some reason. Is there a way to fix it, or do I just need to compensate by add/subtracting numbers?

如何获得EXACT屏幕中心。因此,如果我将 JFrame 默认为200 * 200像素,则中心将为100 * 100。 (允许问题1中的问题)但是如果某人调整屏幕大小(是的,我希望他们能够重新调整大小),那么这一点仍然是中心。因此,如果他们全屏显示,屏幕尺寸为1200 * 900,则中心为600 * 450。

How can I get the EXACT screen center. So, if I make a JFrame which is default as 200*200 pixels, then the center would be 100*100. (Allowing for the problem in question 1) But if someone resizes the screen (and yes, I want them to be able to re-size it) the point is still the center. so if they make it full screen and their screen size is 1200*900, then the center would be 600*450.

我希望这很清楚

推荐答案

从你所说的声音来看,我认为你误解了正常(装饰)框架包括。

From the sounds of what you are saying, I think you misunderstand what the dimensions of a normal (decorated) frame include.

A JFrame 由一个窗口/框架组成(通常用边框装饰) ), JRootPane ,其中包含 JLayeredPane ,其中包含内容窗格 JMenuBar 和玻璃窗格。

A JFrame consists of a window/frame (normally decorated with a border), a JRootPane, which contains a JLayeredPane which contains the content pane, JMenuBar and glass pane.

这是你永远不应该覆盖顶级容器的原因之一 paint 方法,因为你实际上不会在内容/视图区域内绘画。

This is, one of the, reasons why you should never override a top level containers paint method, because you won't actually be painting within the content/view area.

因此,帧的实际可绘制区域是 width - border.width x heig ht - border.height

So, the actual "paintable" region of a frame is it's width - border.width x height - border.height

红线表示框架,蓝色表示内容窗格。

The red line indicates the frame, the blue indicates the content pane.

这提出了一个非常重要的问题,框架的中心在哪里?从框架的角度来看,它是100x100,但从内容窗格的角度来看,它是92x81。根据您的需要,取决于您将使用的值。要定位框架,您需要使用框架中心点进行绘画,您将需要使用内容窗格。

This raises a very important question, where's the center of the frame? From the frames perspective, it's 100x100, but from the content pane's perspective, it's 92x81. Depending on what you want, will depend on which value you will use. For positioning the frame, you will want to to use the frames center point, for painting, your will want to use the content panes.

现在,最简单的中心方式屏幕上的一个框架只是调用 Window #setLocationRelativeTo(null) 否则,我建议你使用Timr的解决方案;)

Now, the easiest way to center a frame on the screen is simply to call Window#setLocationRelativeTo(null) otherwise, I would suggest you use Timr's solution ;)

这篇关于即使在重新调整大小时,如何获得屏幕的中间位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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