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

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

问题描述

我有一个用 Java 制作的简单游戏.本质上,我的编程能力比较基础,我希望能够调整游戏所在窗口的大小,而不是让图像停留在原处,而只是在更大的区域显示白度..

为了使这项技术发挥最佳效果:

  1. 确保执行游戏渲染的组件具有首选大小集.
  2. 将它(和所有其他控件)添加到框架中.
  3. 调用 frame.pack() 以确保框架的大小适合显示内容.
  4. (最后)调用 frame.setResizable(false).

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.

Resize the game space

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?

Pad the game space

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.

Fix the size of the top-level container.

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

For this technique to work best:

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