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

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

问题描述

好的,这种问题有两个部分.

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 屏幕中心.所以,如果我制作一个默认为 200*200 像素的 JFrame,那么中心将是 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.

一个 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 height - 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天全站免登陆