简体   繁体   English

如何让我的JWindow窗口始终保持专注

[英]How do I make my JWindow window always stay focused

I am making a java application that includes a JWindow. 我正在制作一个包含JWindow的java应用程序。 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 对于为什么要在离开JWindow继续处理鼠标,你的问题有点模糊......但是

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 . 当您在应用程序之外设置鼠标时,您有两个(基本)选择,您可以使用JNI / JNA解决方案,也可以轮询MouseInfo

The following demonstrates the latter, using MouseInfo and a javax.swing.Timer to update a label... 下面演示了后者,使用MouseInfojavax.swing.Timer来更新标签......

在此输入图像描述

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 如果支持平台,您还可以找到Window#setAlwaysOnTop的帮助以保持窗口在其他人的Window#setAlwaysOnTop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM