How do I make my JWindow window always stay focused

北慕城南 提交于 2019-12-17 16:54:32

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/18885247/how-do-i-make-my-jwindow-window-always-stay-focused

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!