如何设置 JFrame 始终处于焦点

发布于 2024-12-22 23:37:38 字数 1013 浏览 1 评论 0原文

我想制作一个始终具有焦点并且在关闭之前不会失去焦点的 JFrame,我已经将其设置为始终位于顶部并且我尝试了此代码:

import java.awt.event.*;
import javax.swing.*;

public class Viewer extends JFrame WindowFocusListener
{
    public Viewer()
    {
        addWindowFocusListener(this);
        setAlwaysOnTop(true);
        this.setFocusable(true);
        this.setFocusableWindowState(true);
        panel = new JPanel();

        setSize(WIDTH,HEIGHT);
        setLocation(X,Y);
        setResizable(false);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e){}
    public void windowLostFocus(WindowEvent e)
    {
        toFront();
        requestFocusInWindow();
        System.out.println("focus lost");
    }

    private JPanel panel;
    private static final int WIDTH = 200;
    private static final int HEIGHT = 200;
    private static final int X = 100;
    private static final int Y = 100;

    public static void main(String args[]){new Viewer();}
}

我不明白为什么此代码不起作用,任何人都可以看看我做错了什么?提前致谢。

I want to make a JFrame that always has focus and cannot lose focus until it is closed, i already have it set to be always on top and i tried this code:

import java.awt.event.*;
import javax.swing.*;

public class Viewer extends JFrame WindowFocusListener
{
    public Viewer()
    {
        addWindowFocusListener(this);
        setAlwaysOnTop(true);
        this.setFocusable(true);
        this.setFocusableWindowState(true);
        panel = new JPanel();

        setSize(WIDTH,HEIGHT);
        setLocation(X,Y);
        setResizable(false);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e){}
    public void windowLostFocus(WindowEvent e)
    {
        toFront();
        requestFocusInWindow();
        System.out.println("focus lost");
    }

    private JPanel panel;
    private static final int WIDTH = 200;
    private static final int HEIGHT = 200;
    private static final int X = 100;
    private static final int Y = 100;

    public static void main(String args[]){new Viewer();}
}

I don't see why this code won't work, can anyone see what I'm doing wrong? thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-12-29 23:37:38

这对于简单的 JFrame 来说是不可能的;这是 JDialog 的工作。请参阅这些实现的方法:

setModal ()

setModalityTypes()

This is not possible with a simple JFrame; this is a job for JDialog. Please see these implemented methods:

setModal()

setModalityTypes()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文