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();
    }
}
相关推荐
源码云商1 小时前
基于Spring Boot + Vue的母婴商城系统( 前后端分离)
java·spring boot·后端
学习编程的gas4 小时前
C++面向对象编程入门:从类与对象说起(一)
开发语言·c++
冼紫菜4 小时前
【Spring Boot 多模块项目】@MapperScan失效、MapperScannerConfigurer 报错终极解决方案
java·开发语言·mybatis
还听珊瑚海吗5 小时前
基于SpringBoot的抽奖系统测试报告
java·spring boot·后端
Bear on Toilet5 小时前
Bug日记——实现“日期类”
开发语言·c++·bug
练习本5 小时前
Android系统架构模式分析
android·java·架构·系统架构
apcipot_rain5 小时前
《面向对象程序设计-C++》实验五 虚函数的使用及抽象类
开发语言·c++
明月看潮生7 小时前
青少年编程与数学 02-019 Rust 编程基础 05课题、复合数据类型
开发语言·青少年编程·rust·编程与数学
心灵宝贝7 小时前
IDEA 安装 SpotBugs 插件超简单教程
java·macos·intellij-idea