[英]JFrame in full screen without undecorated
我在使用最新社区版IntelliJ开发的Windows计算机上使用Java8。 为了使JFrame全屏显示,我在下面的解决方案中找到了要验证的一种不同行为。
我从全屏JFrame中获取的解决方案
根据解决方案,我需要在下面三行放置JFrame全屏显示:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
但是在我的项目中,我创建了一个扩展JFrame的类AppFrame.java。 在默认构造函数中,我设置了一些基本属性,例如font等,重要的是将可见性设置为true。
import javax.swing.*;
import java.awt.*;
public class AppFrame extends JFrame {
AppFrame() {
Font baseFont = new Font("Dialog", Font.PLAIN, 12);
setFont(baseFont);
setLocationRelativeTo(null);
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
当我尝试放置三行以上(带有或不带setVisible,这已经来自AppFrame)以最大化它时,在扩展AppFrame的类中出现以下错误:
Exception in thread "main" java.awt.IllegalComponentStateException: The frame is displayable. at java.awt.Frame.setUndecorated(Frame.java:923)
作为解决方案的一部分(我想验证)-实验上,我从AppFrame.java中删除了setVisible(true),并且它起作用了,但这将影响扩展AppFrame的所有类,因此我删除了frame.setUndecorated(true);
以及我班上的内容,并在AppFrame中放回setVisible。 而且异常消失了。 也是frame.setUndecorated(true);
我相信删除JFrame的标题栏。
另外,以下是JFrame的javadoc的摘录:
框架可以使用setUndecorated关闭其本机装饰(即Frame和Titlebar)。 仅当框架无法显示时才能执行此操作。
如果有人可以验证此行为,那就太好了。
根据设计,您必须在setVisible
之前且仅在setVisible
之前调用setUndecorated
。 因此,您别无选择,只能从基类调用setVisible
删除,并在子类中每次都调用它。
修改初始化程序以使用参数。 AppFrame() {
应该更改为AppFrame(boolean undecorated, boolean visible) {
然后在初始化程序中添加setUndecorated(undecorated);
和setVisible(visible);
完成的解决方案:
import javax.swing.*;
import java.awt.*;
public class AppFrame extends JFrame {
AppFrame(boolean undecorated, boolean visible) {
Font baseFont = new Font("Dialog", Font.PLAIN, 12);
setFont(baseFont);
setLocationRelativeTo(null);
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(new FlowLayout());
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(undecorated);
setVisible(visible);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
关于:
框架可以使用setUndecorated关闭其本机装饰(即Frame和Titlebar)。 仅当框架无法显示时才能执行此操作。
这只是说明您必须在调用setVisible(true);
之前执行此操作setVisible(true);
。 要确定是否可以安全地调用setUndecorated
,可以使用if (!isDisplayable()) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.