当JFrame处于全屏模式时显示标题栏不起作用

时间:2017-05-30 11:19:00

标签: java swing jframe

我刚刚编写了一个程序,在全屏模式下显示JFrame ,没有标题栏 。然而,这使JFrame难以关闭。 (用户必须按Alt + F4才能执行此操作)。我想要的是当用户将鼠标移动到JFrame的顶部(比如从顶部开始的1个像素)时,会出现标题栏。这与全屏窗口在Windows 10中具有的功能相同。这是我尝试过的:

import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class fullscreen_test extends JFrame implements MouseMotionListener{
    public fullscreen_test() {
        this.setUndecorated(true); //remove title bar
        this.setAlwaysOnTop(true); //always on top!!!
        this.setResizable(false); //unresizable
        this.setVisible(true);
        int xsize =(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int ysize = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setSize(xsize, ysize); //set the size equal to the screen size
        this.addMouseMotionListener(this); //mousemotionlistener to show title bar
    }
    public static void main(String[] args) {
        fullscreen_test ft = new fullscreen_test();
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        Object src = e.getSource();
        if (e.getY() <= 1){
            if(src instanceof JFrame){
                ((JFrame) src).setUndecorated(false); //This is where eclipse says the error is.
            }
        }
    }
    public void mouseDragged(MouseEvent arg0) {} // required by the MouseMotionListener interface
}

但此代码会产生以下错误:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
at java.awt.Frame.setUndecorated(Unknown Source)

我完全被这个错误所困扰。任何帮助将不胜感激。感谢您的关注。

2 个答案:

答案 0 :(得分:0)

我认为你必须在更改装饰状态之前处理你的JFrame。

尝试类似:

@Override
public void mouseMoved(MouseEvent e) {
    Object src = e.getSource();
    if (e.getY() <= 1){
        if(src instanceof JFrame){
            JFrame frame = (JFrame) src;
            frame.dispose();
            frame.setUndecorated(false);
            frame.setVisible(true);
        }
    }
}

mouseMoved方法中。

JFrame Documentation for dispose()所说的是一个电话

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children.

然后可以操纵资源

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. [...]

在这个/你的案例setVisible(true)中,同样的事情是什么

[...] The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed [...]

通过setUndecorated(true)来电

[...] (not accounting for additional modifications between those actions).

希望它对你有用。 否则请不要犹豫,评论这篇文章!

答案 1 :(得分:0)

正如documentation所述:

  

只有在帧不可显示时才能调用此方法。

稍后在同一文档中:

  

投掷: IllegalComponentStateException - 如果框架可显示

因此,根本无法在可见的未修饰窗口上显示原生标题栏。

然而......你可以通过跨平台的外观来展示一个“假的”标题栏:

((JFrame) src).getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

这是通过外观而不是本机桌面系统完成的,因此不需要重新创建本机窗口。缺点是标题栏看起来不像原生标题栏。