3 回答

TA贡献1860条经验 获得超9个赞
试试这个Window类的方法:
Window.setAlwaysOnTop(boolean)
它的工作方式与Windows TaskManager中的默认工作方式相同:切换到另一个应用程序,但始终显示在最前面。
这是在Java 1.5中添加的
样例代码:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Annoying {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello!!");
// Set's the window to be "always on top"
frame.setAlwaysOnTop( true );
frame.setLocationByPlatform( true );
frame.add( new JLabel(" Isn't this annoying?") );
frame.pack();
frame.setVisible( true );
}
}
即使未激活,窗口仍位于顶部

TA贡献1873条经验 获得超9个赞
从我的观察中,我发现AlwaysOnTop特权已授予要求始终位于最前面的最新进程。
因此,如果您有一个应用程序,setAlwaysOnTop(true)后来又有另一个应用程序使用此选项,则将特权授予第二个应用程序。为了解决此问题,我在任何窗口位于当前窗口顶部时都设置了setAlwaysOnTop(false)和setAlwaysOnTop(true)。
我已经检查了它wordweb在windows。WordWeb是使用以下AlwaysOnTop选项的应用程序之一OS
我不确定它是否适合您的游戏场景。
警告:我不知道副作用。
这是代码示例:
import java.awt.event.*;
import javax.swing.*;
public class MainWindow extends JFrame implements WindowFocusListener
{
public MainWindow()
{
addWindowFocusListener(this);
setAlwaysOnTop(true);
this.setFocusable(true);
// this.setFocusableWindowState(true);
panel = new JPanel();
//setSize(WIDTH,HEIGHT);
setUndecorated(true);
setLocation(X,Y);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
public void windowGainedFocus(WindowEvent e){}
public void windowLostFocus(WindowEvent e)
{
if(e.getNewState()!=e.WINDOW_CLOSED){
//toFront();
//requestFocus();
setAlwaysOnTop(false);
setAlwaysOnTop(true);
//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 MainWindow();}
}
添加回答
举报