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();
    }
}
相关推荐
☆璇2 分钟前
【C++】C/C++内存管理
c语言·开发语言·c++
愿你天黑有灯下雨有伞13 分钟前
枚举策略模式实战:优雅消除支付场景的if-else
java·开发语言·策略模式
网络安全打工人17 分钟前
CentOS7 安装 rust 1.82.0
开发语言·后端·rust
楚轩努力变强19 分钟前
前端工程化常见问题总结
开发语言·前端·javascript·vue.js·visual studio code
Dcs40 分钟前
gRPC性能陷阱:低延迟网络下的客户端瓶颈揭秘
java
梦想的初衷~1 小时前
MATLAB近红外光谱分析技术及实践技术应用
开发语言·支持向量机·matlab
探索java1 小时前
深入解析 Spring 获取 XML 验证模式的过程
xml·java·spring
找不到、了1 小时前
Java设计模式之<装饰器模式>
java·设计模式·装饰器模式
Fly-ping1 小时前
【前端】JavaScript文件压缩指南
开发语言·前端·javascript
java叶新东老师1 小时前
解决windows系统下 idea、CLion 控制台中文乱码问题
java·windows·intellij-idea