简体   繁体   English

JFrame大小,调整大小,全屏

[英]JFrame size, resize, fullscreen

My question have 3 parts. 我的问题分为三个部分。

  1. I would like to know, how can I set JFrame window to be resizable, but can not go under a minim size so the user cant make it smaller then the minimum size. 我想知道,如何将JFrame窗口设置为可调整大小,但不能设置为最小大小,因此用户无法将其设置为小于最小大小。
  2. I would like to know, how can I set JFrame window to be resizable, but still keep the aspect ratio (16:9) even if the user is resizing it. 我想知道,如何将JFrame窗口设置为可调整大小,但是即使用户正在调整它的大小,仍要保持宽高比(16:9)。
  3. I would like to know, how can I make the program become really full screen after he clicks a button so there are no borders and runs like a game or something (and if there is any specific issues how can I revert it back safely). 我想知道,在他单击按钮后如何使程序真正变成全屏显示,这样就没有边框了,并且像游戏之类的东西一样运行(并且如果有任何特定的问题,我该如何安全地还原它)。

Thanks for your help and patient for my not perfect English and not perfect Java knowledge. 感谢您的帮助和耐心,因为我的英语水平还不太好,Java知识也不很完善。

The provided answer is useful and answers part of my question, but still most of it still blank for me. 提供的答案很有用,可以回答我的部分问题,但对于我来说,大多数仍然空白。

The part of the code: 代码部分:

private void createDisplay() {
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    frame = new JFrame(title);                                              //setting the title of the window
    frame.setSize(width, height);                                           //setting the width and height of the window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                   //make sure program closes down properly
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);                                      //the window will apparel on the center
    frame.setVisible(true);
    frame.setIconImage(Assets.ImageMap.get("icon"));

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(gd.getDisplayMode().getWidth() / 2, gd.getDisplayMode().getHeight() / 2));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.pack();                                                           //resize the window a little bit so able to see all of canvas
}
  1. Set the frame's minimum size (setMinimumSize) 设置框架的最小尺寸(setMinimumSize)
  2. Is more difficult, as you are notified of the size change after the fact, so you'll always be fighting the system as it changes (and then you run into problems with who generated the size change event). 更加困难,因为事后会通知您大小更改,因此您将始终在系统更改时与系统进行斗争(然后您会遇到由谁生成大小更改事件的问题)。 It might be better to focus on keeping the ratio of the contents of the frame based on the available space within the frame (VLC is a good example of this) 最好根据框架内的可用空间集中于保持框架内容的比率(VLC是一个很好的例子)
  3. Full screen exclusive mode 全屏独占模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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