简体   繁体   English

如何编写 jButton 以将笔记本电脑或设备切换到节电模式

[英]How to code a jButton to turn the laptop or device to battery saver mode

I haven't tried any coding, because I'm inexperienced.我没有尝试过任何编码,因为我没有经验。

But for an application I'm currently making (for an assignment) I want to enable the user to turn the battery saver mode when the count down ended, (if the user previously selected the 'turn battery saver when resting' checkbox) the power-saver mode will be automatically enabled by the software.但是对于我目前正在制作的应用程序(用于分配),我想让用户在倒计时结束时打开省电模式,(如果用户之前选择了“休息时打开省电模式”复选框)电源-saver 模式将由软件自动启用。 (OS - Windows 10). (操作系统 - Windows 10)。

You can implement such an action by executing a cmd command via the ProcessBuilder .您可以通过ProcessBuilder执行 cmd 命令来实现此类操作。 I have put together a program that works for me:我整理了一个适合我的程序:

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();
      }
    }
  });
}

Maybe you need to change the command "rundll32.exe powrprof.dll,SetSuspendState" to something else.也许您需要将命令“rundll32.exe powrprof.dll,SetSuspendState”更改为其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM