简体   繁体   中英

How can I resize my window in Java without affecting the dimensions?

I'm trying to make a monopoly game in java, the thing is i'm very new to programming and this is my first program.

So without knowing about the dimensions of the frame, i made it too big because it was okay for my screen resolution, but when I try to use the game in another computer, it is so big that it doesn't even fit in the screen.

So my question is how can I make that my game can resize automatically so I can use it in another PC without affecting its dimensions?

Thank you very much :)

Assuming that you're creating a Swing application (you never say), the is to not size any of the components directly but rather to use an appropriate mix of layout managers, and let the layout managers and the preferredSize of your components do the sizing of the GUI for you. Always remember to add the components to the GUI, then call pack() on the top level Window, and then call setVisible(true) on the top level window.

For more details on just what layouts are available through core Java, please check out the tutorial, Laying out Components in a Container .

If your question does not involve the Swing GUI library, then please let us know which GUI library you're using. The question details are quite important, and you'll want to tell them to us from the start if possible.

如果您要使游戏全屏运行,这可能会有所帮助: 如何在Java中获得屏幕分辨率?

Simulating such a game is not easy; you might want to start with something easier, such as MVCGame , which places components in a simple layout, or Buttons , which adapts the font size to the available space at a given level of difficulty.

For continuous scaling, you'll need to scale() the graphics context's transform to fill the drawing surface, as shown here . For rotated text, the examples shown here may be helpful.

window.setExtendedState(JFrame.MAXIMIZED_BOTH); sets the window to full screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM