Lap*_*son 85 java user-interface swing awt
在Java中,有没有办法让一个窗口"永远在顶部",无论用户是否将焦点切换到另一个应用程序?我搜索过网络,所有解决方案都倾向于使用本机绑定的某种JNI接口.真的,这不是唯一的方法吗?..或者是吗?
Osc*_*Ryz 157
试试这个Window类的方法:
它的工作方式与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 );
}
}
Run Code Online (Sandbox Code Playgroud)
即使未激活,窗口仍保持在顶部
pin*_*her 11
根据我的观察,我发现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();}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77882 次 |
| 最近记录: |