如何设置 JFrame 始终处于焦点
我想制作一个始终具有焦点并且在关闭之前不会失去焦点的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对于简单的
JFrame
来说是不可能的;这是 JDialog 的工作。请参阅这些实现的方法:setModal ()
setModalityTypes()
This is not possible with a simple
JFrame
; this is a job forJDialog
. Please see these implemented methods:setModal()
setModalityTypes()