//下面是编译命令
// javac -encoding utf-8 Main.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends WindowAdapter {
JLabel label = null;
public Main() {
JFrame jFrame = new JFrame("保持打开就不会锁屏");
Container containerPanel = jFrame.getContentPane();
containerPanel.setLayout(new GridLayout(2, 1));
JButton button = new JButton("请保持程序运行");
label = new JLabel("by pqyang");
containerPanel.add(button);
containerPanel.add(label);
jFrame.setSize(200, 200);
jFrame.setVisible(true);
jFrame.addWindowListener(this);
Runnable runnable = new Runnable() {
Robot robot = null;
@Override
public void run() {
try {
robot = new Robot();
while (true) {
robot.keyPress(KeyEvent.VK_SCROLL_LOCK);
robot.keyRelease(KeyEvent.VK_SCROLL_LOCK);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AWTException e) {
e.printStackTrace();
}
}
};
Thread t = new Thread(runnable);
t.start();
}
public static void main(String[] args) {
new Main();
}
}