问题描述
我正在Windows机器上使用Java8,该机器是通过IntelliJ的最新社区版本开发的.为了使JFrame全屏显示,我在下面的解决方案中找到了要验证的一种不同行为.
I am using Java8 on Windows machine developing with latest community edition of IntelliJ. To make JFrame full screen I find below solution where I faced one different behavior which I want to get verified.
根据解决方案,我需要在下面三行输入内容,以使JFrame全屏显示:
As per the solution I need to put three below lines to make JFrame full screen:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
但是在我的项目中,我创建了一个扩展JFrame的类AppFrame.java.在默认的构造函数中,我设置了一些基本属性,例如font等,重要的是将可见性设置为true.
But in my project I have created a class AppFrame.java that extends JFrame. And in default constructor I set some basic properties like font etc and importantly visibility to 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);
}
}
并且在扩展AppFrame的类中,当我尝试放置三行以上(带有或不带有setVisible,这已经来自AppFrame)以最大化它时,出现以下错误:
And in class that extends AppFrame when I try to put above three lines (with or without setVisible, which is already coming from AppFrame) to maximize it get below error:
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);
从我的班级中放回setVisible在AppFrame中.而且异常消失了.另外,我相信frame.setUndecorated(true);
也会删除JFrame的标题栏.
As a part of solution (which I want to verify) - Experimentally I removed setVisible (true) from AppFrame.java and it worked, but this would be gonna impact all classes extending AppFrame, so I removed frame.setUndecorated(true);
as well from my class and put back setVisible in AppFrame. And exception is gone. Also frame.setUndecorated(true);
I believe removes title bar of JFrame.
此外,以下是JFrame的javadoc的摘录:
Also, below is excerpt from javadoc of JFrame:
如果有人可以验证此行为,那就太好了.
It would be great if someone can verify this behavior.
推荐答案
修改初始化程序以使用参数.AppFrame() {
应该更改为AppFrame(boolean undecorated, boolean visible) {
,然后在初始化程序中添加setUndecorated(undecorated);
和setVisible(visible);
Modify the initializer to use parameters.AppFrame() {
should be changed to AppFrame(boolean undecorated, boolean visible) {
then in the initializer add setUndecorated(undecorated);
and 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);
}
}
关于:
这只是说明您必须在调用setVisible(true);
之前执行此操作.要确定是否可以安全地呼叫setUndecorated
,可以使用if (!isDisplayable()) { ... }
This is just stating that you have to do this before calling setVisible(true);
. To determine if you can call setUndecorated
safely, you can use if (!isDisplayable()) { ... }
这篇关于全屏显示JFrame,无需修饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!