如何编写jButton代码以将笔记本电脑或设备转换为省电模式



我没有尝试过任何编码,因为我没有经验。

但是对于我目前正在制作的应用程序(对于作业(,我想使用户能够在倒计时结束时打开节电模式,(如果用户之前选中了"休息时打开节电模式"复选框(省电模式将由软件自动启用。(操作系统 - 视窗 10(。

您可以通过 ProcessBuilder 执行 cmd 命令来实现此类操作。 我整理了一个适合我的程序:

public static void main(String[] args) throws Exception {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
JButton button = new JButton("Standby");
panel.add(button);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "rundll32.exe powrprof.dll,SetSuspendState");
builder.redirectErrorStream(true);
try {
builder.start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}

也许您需要将命令"rundll32.exe powrprof.dll,SetSuspendState"更改为其他内容。

最新更新