我希望创建一个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();}
}
我不明白为什么这段代码不能运行,请问有谁能看出我的问题所在吗?提前感谢。