JFrame doesn#39;t take the actual screen size(JFrame 不采用实际的屏幕尺寸)
问题描述
我一直在尝试将 JFrame 的大小设置为与屏幕大小完全相同 (2256x1504).它似乎也采用了那个大小,但是当我展示某些东西时,结果总是比预期的要大.
I have been trying to set the size of my JFrame to the exact same size of my screen (2256x1504). It also seems to take that size but when I display something the outcome is always bigger than intended.
public class Frame extends JFrame {
public Frame() {
JPanel p = new JPanel();
int width = 2256;
int height = 1504;
Dimension size = new Dimension(width,height);
p.setPreferredSize(size);
p.setMinimumSize(size);
p.setMaximumSize(size);
p.setSize(size);
this.setUndecorated(true);
this.setContentPane(p);
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setVisible(true);
}
}
推荐答案
int width = 2256;
int height = 1504;
猜测您的屏幕是 2556x1504.你给框架这个尺寸.当我们全屏显示程序时(从默认按钮 OS 在窗口右上角给出),它不会获得屏幕大小.它需要屏幕的大小 - 操作系统任务栏的高度.其他操作系统我不知道,但是如果你想在windows中做一个不装饰的框架全屏并涉及到任务栏,你必须使用这个:
Guessing your screen is 2556x1504. You give the frame this size. When we full screen a program (from default button OS gives in upper right corner of the window), it does not get screen's size. It takes screen's size - the height of the OS task bar. I do not know for other operating systems, but if you want to make an undecorated frame full screen in windows and involve the task bar, you have to use this:
public class Frame extends JFrame {
public Frame() {
JPanel p = new JPanel();
int width = 2256;
int height = 1504;
Dimension size = getScreenDimensionWithoutTaskbar(this);
p.setPreferredSize(size);
p.setMinimumSize(size);
p.setMaximumSize(size);
p.setSize(size);
this.setUndecorated(true);
this.setContentPane(p);
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setVisible(true);
}
public static Dimension getScreenDimensionWithoutTaskbar(Frame frame) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());
int taskBarSize = screenInsets.bottom;
return new Dimension(width, height - taskBarSize);
}
public static void main(String[] args) {
new Frame();
}
}
这篇关于JFrame 不采用实际的屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFrame 不采用实际的屏幕尺寸
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01