I am new to java development, and decided to create a simple java project that outputs the X and Y coordinates of where the mouse is inside of a jframe. I am unsure how I would use a listener.
package main;
import java.awt.BorderLayout;
import java.awt.MouseInfo;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Main {
public static void main(String[] args) {
jFrame();
}
public static void jFrame() {
JFrame myFrame = new JFrame("Screen Location");
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 300, 100);
JLabel myText = new JLabel(""+ getTotal(),
SwingConstants.CENTER);
myFrame.getContentPane().add(myText, BorderLayout.CENTER);
}
private static String getTotal() {
for (int i = 0; i < 1;) {
int publicScreenValueX = MouseInfo.getPointerInfo().getLocation().x;
int publicScreenValueY = MouseInfo.getPointerInfo().getLocation().y;
String total = new String(publicScreenValueX + ", " + publicScreenValueY);
return total;
}
return null;
}
}
How would I be able to make the project constantly update the coordinates? Could someone provide a reference on how to do this?
Do note, this is without using a button, or without any input from the user, other than moving the mouse.
Here is an image of what this would look like:
Except the coordinates would always be updating.