Java实用办公小程序

文章目录

识别鼠标位置

java 复制代码
import java.awt.*;
import java.awt.event.InputEvent;

public class MousePositionTracker {
    public static void main(String[] args) throws AWTException, InterruptedException {
        Robot robot = new Robot();

        while (true) {
            // Get the current mouse position
            Point mousePosition = MouseInfo.getPointerInfo().getLocation();
            int x = (int) mousePosition.getX();
            int y = (int) mousePosition.getY();

            // Print the coordinates to the console
            System.out.println("Mouse position: (" + x + ", " + y + ")");

            // Wait for 1 second
            Thread.sleep(1000);
        }
    }
}

对电脑指定坐标进行点击

java 复制代码
import java.awt.*;
import java.awt.event.InputEvent;

public class ScreenClicker {
    public static void main(String[] args) throws AWTException {
        Robot robot = new Robot();

        int x = 673;
        int y = 723;

        for (int i = 0; i < 20; i++) {
            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);  // Press the left mouse button
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // Release the left mouse button
            try {
                Thread.sleep(1000); // Wait for 1 second between clicks
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Clicked on location (" + x + ", " + y + ")");
    }
}

用户按下指定按键对程序做出终止(注意英文输入法才可以识别)

java 复制代码
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyPressDetector implements KeyListener {
    private JFrame frame;

    public KeyPressDetector() {
        // Create a JFrame window
        frame = new JFrame("Key Press Detector");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        // Register the KeyListener to the JFrame window
        frame.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // keyTyped event is triggered when a character is typed
        // This method is not used in this example, but it's required to implement the KeyListener interface
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // keyPressed event is triggered when a key is pressed
        if (e.getKeyCode() == KeyEvent.VK_Q) {
            System.out.println("Exiting the program.");
            System.exit(0); // Exit the program
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // keyReleased event is triggered when a key is released
        // This method is not used in this example, but it's required to implement the KeyListener interface
    }

    public static void main(String[] args) {
        // Create an instance of KeyPressDetector to start the program
        new KeyPressDetector();
    }
}
相关推荐
蜡台3 小时前
Kotlin 五大作用域函数详解|let/run/apply/also/with 选型指南+实战避坑
android·java·kotlin
haolin123.3 小时前
STL vector底层揭秘:从构造到迭代器失效
开发语言·c++·算法
大模型码小白3 小时前
ChatGPT 的回答也能被结构化抓取?AI Bot Scraper 对比测评
服务器·开发语言·数据库·人工智能·spring·chatgpt
尘中远3 小时前
给C++工业软件搭建 Agent
开发语言·c++·qt·ai·agent
AC赳赳老秦3 小时前
农产品公开数据应用:OpenClaw 抓取农产品价格、产销公开数据,实现农产品行情动态监测
java·c语言·javascript·python·php·deepseek·openclaw
1001101_QIA3 小时前
从 CPU 到 GPU:高速缓存、指令并行与 CUDA 执行原理入门
java·后端·spring
API快乐传递者3 小时前
淘宝海外商品详情接口实战指南:从全球开放平台到跨境铺货的全链路方案
java·前端·数据库
邪修king3 小时前
Re: Linux系统篇(十八):进程篇(七): 进程深度解析:从 fork 创建到退出的完整旅程(附写时拷贝原理 + 代码实战)
java·linux·运维
m0_587383003 小时前
24小时自助健身系统源码实战:从架构设计到部署落地
java·架构·系统架构·需求分析
卓怡学长3 小时前
w156一周穿搭App的设计与实现
java·spring boot·spring·maven·intellij-idea