如何让我的JWindow窗口始终保持专注 [英] How do I make my JWindow window always stay focused

查看:161
本文介绍了如何让我的JWindow窗口始终保持专注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含JWindow的java应用程序。我希望能够跟踪鼠标而无需用户在转到另一个窗口后点击窗口。

解决方案

您的问题对于为什么要在鼠标离开 JWindow 后继续处理鼠标有点模糊......但是



你当你在应用程序之外设置鼠标时,有两个(基本)选择,你可以使用JNI / JNA解决方案,或者你可以轮询帮助保持窗口在其他人的顶部,如果支持平台


I am making a java application that includes a JWindow. I want to be able to track the mouse without the user having to click the window after going to another window.

解决方案

Your question is little vague on why you want to continue processing the mouse once it's left the JWindow...but

You have two (basic) choices when it comes to mointoring the mouse outside of your application, you can use a JNI/JNA solution or you can poll MouseInfo.

The following demonstrates the latter, using MouseInfo and a javax.swing.Timer to update a label...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseWindow {

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

    public MouseWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {
            setLayout(new BorderLayout());
            label = new JLabel();
            label.setFont(label.getFont().deriveFont(48f));
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            add(label);
            updateMouseInfo();

            Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updateMouseInfo();
                }
            });
            timer.start();
        }

        protected void updateMouseInfo() {
            PointerInfo pi = MouseInfo.getPointerInfo();
            label.setText(pi.getLocation().x + "x" + pi.getLocation().y);
        }            
    }
}

Updated

You may also find Window#setAlwaysOnTop of help to keep the window ontop of the others, if support for the platform

这篇关于如何让我的JWindow窗口始终保持专注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆