我是java开发的新手,并决定创建一个简单的java项目,输出鼠标在jframe内部的X和Y坐标。我不确定如何使用听众。
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;
}
}
我怎样才能让项目不断更新坐标?有人可以提供如何做到这一点的参考吗?
请注意,除了移动鼠标外,没有使用按钮,也没有使用任何输入。
以下是这样的图像:
除了坐标总是会更新。
答案 0 :(得分:0)
基本上,为了创建处理鼠标事件的JFrame,您所要做的就是:
1 - 创建一个扩展JFrame并实现MouseListener
的类2 - @Override mouseClicked,mouseEntered,mouseExited,mousePressed,mouseReleased监视相应的事件。现在,每当发生这些事件中的一个时,相应的功能将启动。 使用MouseEvent.getX(),MouseEvent.getY()来获取鼠标事件发生的窗口的坐标。
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseListener {
private static final long serialVersionUID = 1L;
public CreateJFrameWindowWithMouseEventHandling() {
setTitle("Simple Frame");
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
JOptionPane.showMessageDialog(null, "Mouse Clicked at X: " + String.valueOf(x) + " - Y: " + String.valueOf(y));
}
@Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);
}
@Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);
}
@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Released at X: " + x + " - Y: " + y);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new CreateJFrameWindowWithMouseEventHandling();
//Display the window.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
答案 1 :(得分:0)
我建议创建一个继承自JFrame的新类以及所有这些,因为它更加面向对象友好。
但是,使用您的代码,您需要做的是向您的JFrame添加MouseMotionListener
,实现所有抽象方法,并在方法mouseMoved
上更改您的myText
标签文本;
将此方法替换为您当前拥有的方法,它应该可以工作。
请注意,java.awt.event.MouseMotionListener
仅向您提供有关应用程序窗口内鼠标移动的信息。对于在该窗口之外发生的事件,您可能想要检查另一个问题:
Java mouse motion anywhere on screen
public static void jFrame() {
JFrame myFrame = new JFrame("Screen Location");
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the application close when you close the JFrame
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 300, 100);
JLabel myText = new JLabel(""+ getTotal(), SwingConstants.CENTER);
myFrame.getContentPane().add(myText, BorderLayout.CENTER);
myFrame.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent e) {
// don't delete this method
// all abstract methods must be overridden
}
@Override
public void mouseMoved(MouseEvent e) {
myText.setText(getTotal());
}
});
}