开发者

How to handle Windows XP or VISTA Events in JAVA

开发者 https://www.devze.com 2023-01-03 17:52 出处:网络
I\'d like to ask another question how to handle Windows\' events in Java. To be specific, I\'d like to know how to handle events such as mouse moved or mouse clicked in Windows XP and Vista. I want to

I'd like to ask another question how to handle Windows' events in Java. To be specific, I'd like to know how to handle events such as mouse moved or mouse clicked in Windows XP and Vista. I want to wire my own custom behavior in my applicatio开发者_运维百科n to these events, even when my application is inactive or otherwise hidden.

All help is appreciated!


you can add e.g. a MouseListener to any JComponent by calling

addMouseListener()

There are different EventListeners which you can use instead of MouseListeners

  • KeyListener
  • WindowListener
  • ComponentListener
  • ContainerListener
  • FocusListener
  • ...and many more

Check here for an detailed explanation

you can implement the MouseListener Interface completely or just use the convienience class MouseAdapter, which has method stubs, so you dont have to implement every single method.

check this sample:

public class MyFrame extends JFrame {
    private MouseListener myMouseListener;

        public MyFrame() {
            this.setSize(300, 200);
            this.setLocationRelativeTo(null);
            // create the MouseListener...
            myMouseListener = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("clicked button " + e.getButton() + " on " + e.getX() + "x" + e.getY()); // this gets called when the mouse is clicked.
                }
            };
            // register the MouseListener with this JFrame
            this.addMouseListener(myMouseListener);
        }

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MyFrame frame=new MyFrame();
                    frame.setVisible(true);
                }
            });
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消