JFrame 大小、调整大小、全屏
JFrame size, resize, fullscreen
我的问题分为 3 个部分。
- 我想知道,如何将
JFrame
window 设置为可调整大小,但不能低于最小尺寸,因此用户不能将其设置为小于最小尺寸。
- 我想知道,如何设置
JFrame
window 可调整大小,但即使用户正在调整它的大小仍保持纵横比 (16:9)。
- 我想知道,我怎样才能让程序在他点击一个按钮后变成真正的全屏,这样就没有边框并且像游戏或其他东西一样运行(如果有任何具体问题我该如何恢复它安全返回)。
感谢您的帮助和耐心等待我不完美的英语和不完善的 Java 知识。
提供的答案很有用,回答了我的部分问题,但大部分对我来说仍然是空白。
部分代码:
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
}
- 设置框架的最小尺寸 (setMinimumSize)
- 更难,因为您会在事后收到大小更改的通知,所以您总是会在系统更改时与系统作斗争(然后您 运行 会陷入由谁生成大小更改的问题事件)。最好根据框架内的可用 space 专注于保持框架内容的比例(VLC 就是一个很好的例子)
- Full screen exclusive mode
我的问题分为 3 个部分。
- 我想知道,如何将
JFrame
window 设置为可调整大小,但不能低于最小尺寸,因此用户不能将其设置为小于最小尺寸。 - 我想知道,如何设置
JFrame
window 可调整大小,但即使用户正在调整它的大小仍保持纵横比 (16:9)。 - 我想知道,我怎样才能让程序在他点击一个按钮后变成真正的全屏,这样就没有边框并且像游戏或其他东西一样运行(如果有任何具体问题我该如何恢复它安全返回)。
感谢您的帮助和耐心等待我不完美的英语和不完善的 Java 知识。
提供的答案很有用,回答了我的部分问题,但大部分对我来说仍然是空白。
部分代码:
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
}
- 设置框架的最小尺寸 (setMinimumSize)
- 更难,因为您会在事后收到大小更改的通知,所以您总是会在系统更改时与系统作斗争(然后您 运行 会陷入由谁生成大小更改的问题事件)。最好根据框架内的可用 space 专注于保持框架内容的比例(VLC 就是一个很好的例子)
- Full screen exclusive mode