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();
    }
}
相关推荐
+VX:Fegn089519 小时前
计算机毕业设计|基于springboot + vue物流配送中心信息化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·小程序·课程设计
随丶芯19 小时前
IDEA安装leetcode-editor插件
java·开发语言
范什么特西19 小时前
下载idea旧版本
java·ide·intellij-idea
说私域19 小时前
B站内容生态下的私域流量运营创新:基于AI智能名片链动2+1模式与S2B2C商城小程序的融合实践
人工智能·小程序·流量运营
计算机毕设指导619 小时前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_124987075319 小时前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
小毅&Nora19 小时前
【Java线程安全实战】⑧ 阶段同步的艺术:Phaser 与 Condition 的高阶玩法
java·多线程
Ccjf酷儿19 小时前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
内存不泄露19 小时前
基于Spring Boot和Vue的企业办公自动化系统设计与实现
java·vue.js·spring boot·intellij-idea
禹曦a19 小时前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful