Domanda

So i have this code, however i dont get it how to set the mouse coordinates to the label every time the mouse moves ...

timer.schedule(new TimerTask() {

    @Override
    public void run() {
        int mouseX = MouseInfo.getPointerInfo().getLocation().x;
        int mouseY = MouseInfo.getPointerInfo().getLocation().y;
        lblInfo.setText("Nada "+mouseX+mouseY);
    }

}, 1);

Im not even sure if the code is right but what i want it to do is to get the mouse coordinates in the label called lblInfo every time the mouse moves.

This code what does is only display it once whenever the program starts...

È stato utile?

Soluzione

You need to implements MouseMotionListener, then add your logic inside mouseMoved method like:

public class MyClass implements MouseMotionListener {

    public void mouseMoved(MouseEvent e) {
       System.out.println("X : " + e.getX());
       System.out.println("Y : " + e.getY());
    }

    public void mouseDragged(MouseEvent e) {
       //do something
    }

}

Read more about MouseMotionListener

Altri suggerimenti

Have a look at this example. You first need to implement mousePresseded then mouseDragged. The first to get the point of the initial press, then the mouseDragged will use those coordinates.

addMouseListener(new MouseAdapter() {
     public void mousePressed(MouseEvent me) {
         // Get x,y and store them
         pX = me.getX();
         pY = me.getY();
     }
});
addMouseMotionListener(new MouseAdapter() {
     public void mouseDragged(MouseEvent me) {
         frame.setLocation(frame.getLocation().x + me.getX() - pX, 
              frame.getLocation().y + me.getY() - pY);
     }
});

Complete running example. It uses an undecorate frame and create a JPanel as the header, that you can drag to move the frame.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class UndecoratedExample {
    static JFrame frame = new JFrame();
    static class MainPanel extends JPanel {
        public MainPanel() {
            setBackground(Color.gray);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }

    static class BorderPanel extends JPanel {

        JLabel stackLabel;
        int pX, pY;

        public BorderPanel() {
            ImageIcon icon = new ImageIcon(getClass().getResource(
                    "/resources/stackoverflow1.png"));
            stackLabel = new JLabel();
            stackLabel.setIcon(icon);

            setBackground(Color.black);
            setLayout(new FlowLayout(FlowLayout.RIGHT));

            add(stackLabel);

            stackLabel.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                    System.exit(0);
                }
            });
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent me) {
                    // Get x,y and store them
                    pX = me.getX();
                    pY = me.getY();
                }
            });
            addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent me) {
                    frame.setLocation(frame.getLocation().x + me.getX() - pX, 
                            frame.getLocation().y + me.getY() - pY);
                }
            });
        }
    }

    static class OutsidePanel extends JPanel {

        public OutsidePanel() {
            setLayout(new BorderLayout());
            add(new MainPanel(), BorderLayout.CENTER);
            add(new BorderPanel(), BorderLayout.PAGE_START);
            setBorder(new LineBorder(Color.BLACK, 5));
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                frame.setUndecorated(true);
                frame.add(new OutsidePanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top