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();
    }
}
相关推荐
邪恶的贝利亚19 分钟前
prompt工程起步
开发语言·python·prompt
东雁西飞21 分钟前
MATLAB 控制系统设计与仿真 - 26
开发语言·算法·matlab·工业机器人·智能机器人
知行电子-22 分钟前
MATLAB R2024b 安装教程
开发语言·matlab·信息可视化
大大。32 分钟前
wepy微信小程序自定义底部弹出框功能,显示与隐藏效果(淡入淡出,滑入滑出)
微信小程序·小程序·notepad++
橘猫云计算机设计35 分钟前
基于jspm校园安全管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·安全·毕业设计
幽络源小助理35 分钟前
SpringBoot学生宿舍管理系统的设计与开发
java·spring boot·后端·学生宿舍管理
Clockwiseee35 分钟前
js原型链污染
开发语言·javascript·原型模式
猿java36 分钟前
源码分析:Spring IOC容器初始化过程
java·后端·spring
阿拉保43 分钟前
C++复试笔记(四)
java·c++·笔记
Biomamba生信基地1 小时前
R语言基础| 高级数据管理
开发语言·r语言