调整窗口大小并在其中移动内容 [英] Resize window and move contents within

查看:127
本文介绍了调整窗口大小并在其中移动内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java开发的简单游戏。本质上,我的编程能力是相对基本的,并且我希望能够调整游戏所在的窗口的大小,并且不让图像停留在原处,而只是在较大区域显示白色。这是屏幕截图。有没有简单的方法可以做到这一点?

I have a simple game I made in java. Essentially, my programming ability is relatively basic, and I would like to be able to resize the window the game is in and not have the images stay where they are and just display whiteness in the larger area. Here's a Screenshot. Is there any simple way to do this?

推荐答案

现有的两个答案似乎都假设您希望调整游戏空间的大小。

The two existing answers seem to presume you wish for the game space to resize.

作为射击游戏的游戏空间不一定调整得很好。例如。如果游戏的宽度是原来的两倍,是否应该按向左/向右箭头将飞船移动两倍?垂直拉伸窗户会增加重力或射击速度吗?

A game space that is a shoot'em'up does not necessarily resize well. E.G. When the game is twice as wide, should the left/right arrow presses move the ship twice as fast? Does stretching the window vertically increase gravity, or the speed of shots?

我阅读问题的方式只是希望将游戏空间居中窗户。可以在此答案中看到其EG。

The way I read the question, you simply wish to center the game space in the window. E.G.s of that can be seen in this answer.

< img src = https://i.stack.imgur.com/QsSEU.png>

许多游戏都调整了大小,以避免第一种方法产生的问题类型,并避免第二种方法周围的大空白。这就像调用 setResizable(false)

Many games fix the size to avoid the types of problems created by the 1st approach, and avoid the 'big blank bands' around the 2nd approach. This is as simple as calling setResizable(false).

此技术最有效:


  1. 确保进行游戏渲染的组件具有首选的大小设置。

  2. 将其(以及所有其他控件)添加到

  3. 调用 frame.pack()以确保该框架的大小适合显示内容。

  4. (最后)调用 frame.setResizable(false)

  1. Ensure the component doing the game rendering has a preferred size set.
  2. Add it (and all other controls) to the frame.
  3. Call frame.pack() to ensure the frame is the right size to display the content.
  4. (Finally) call frame.setResizable(false).

这篇关于调整窗口大小并在其中移动内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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