如何使用 Java MouseEvent 数据
很抱歉,如果这不是正确的网站,但我最近一直在学习 Java,并且开始关注 MouseEvents。我已经让它与控制台中输出的数据一起工作,但我不知道如何使用这些数据。这是输出的数据:
java.awt.event.MouseEvent[MOUSE_RELEASED,(4,26),absolute(4,26),button=1,modifiers=Button1,clickCount=1] on frame0
我需要访问数据中的“(4,26)”,但我不知道如何处理。数据的格式是MouseEvent。
谢谢,如果这是错误的网站,再次抱歉。
编辑:这是使用的代码:
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new main());
jf.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e) {
// Mouse Pressed
}
@Override
public void mouseExited(MouseEvent e) {
// Mouse Exit
}
@Override
public void mouseEntered(MouseEvent e) {
// Mouse Enter
}
@Override
public void mouseClicked(MouseEvent e) {
// General Click
}
});
jf.setSize(Settings.map_width, Settings.map_height);
jf.setTitle(Settings.frame_name);
jf.setResizable(Settings.frame_resize);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
Sorry if this isn't the right website for this, but I've been learning Java lately and I have started to look at MouseEvents. I have got it working with the Data being output in the Console, but I have no idea how to use this Data. This is the data being output:
java.awt.event.MouseEvent[MOUSE_RELEASED,(4,26),absolute(4,26),button=1,modifiers=Button1,clickCount=1] on frame0
I need to access the "(4,26)" in the data, but I don't know how to go about this. The format of the data is MouseEvent.
Thanks, and again, sorry if this is the wrong website.
EDIT: This is the code used:
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new main());
jf.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e) {
// Mouse Pressed
}
@Override
public void mouseExited(MouseEvent e) {
// Mouse Exit
}
@Override
public void mouseEntered(MouseEvent e) {
// Mouse Enter
}
@Override
public void mouseClicked(MouseEvent e) {
// General Click
}
});
jf.setSize(Settings.map_width, Settings.map_height);
jf.setTitle(Settings.frame_name);
jf.setResizable(Settings.frame_resize);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看 MouseEvent 的 Javadocs 您将看到它有多种可用于从事件获取信息的方法。
例如,
e.getX()
将返回4
,e.getY()
将返回26
您发布的代码和事件示例。If you look at the Javadocs for MouseEvent you'll see that it has a number of methods you can use to get information from the event.
e.getX()
for example, would return4
ande.getY()
would return26
given the code and event example you posted.您检查过 java docs 吗?您会知道您想要获取的数据是通过以下方式获得的:
Had you checked the java docs, you would have known that the data you want to get is obtained by: