本帖最后由 Sunday 于 2014-4-14 17:59 编辑
- package cn.gao;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class GetClick extends JFrame {
- /**
- * 这个是获取鼠标在面板中的点击事件的源代码,但是我想要获取鼠标在"屏幕"中点击时的“坐标”
- * 请问JAVA中怎么获取鼠标在"屏幕"中点击时的“坐标”和“时刻点”,
- * 希望能提供代码,谢谢!!!
- */
- private static final long serialVersionUID = 1L;
- public static void main(String[] args) {
- JFrame jf = new JFrame("点击面板获取坐标");
- final JLabel jl = new JLabel("点击");
- jf.add(jl);// 添加JLabel
- jf.addMouseListener(new MouseListener() {// 为面板添加监听
- public void mouseClicked(MouseEvent e) {
- Date now = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");// 设置时间格式
- if (e.getButton() == MouseEvent.BUTTON1) {// 判断如果是鼠标左键
- jl.setText("坐标:" + e.getX() + "," + e.getY() + "时间:"
- + dateFormat.format(now));// 修改Label的值
- }
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- });
- jf.setVisible(true);// 设置可见
- jf.setBounds(0, 0, 500, 400);// 设置大小
- }
- }
复制代码 |