swing

作者: 雪国落叶 | 来源:发表于2024-10-10 20:10 被阅读0次
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class HelloWorldSwing {
    public static void main(String[] args) {
        // 创建并显示GUI
        SwingUtilities.invokeLater(HelloWorldSwing::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("管理员权限操作");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setLayout(new BorderLayout());

        // 创建说明文本
        JLabel label = new JLabel("点击按钮以管理员身份执行命令:", JLabel.CENTER);
        frame.add(label, BorderLayout.CENTER);

        // 创建按钮
        JButton button = new JButton("执行 bcdedit 命令");
        button.addActionListener(e -> executeCommandWithAdminRights());
        frame.add(button, BorderLayout.SOUTH);

        // 显示窗口
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static void executeCommandWithAdminRights() {
        try {
            // 创建批处理文件
            File batchFile = new File("increase_user_va.bat");
            try (FileWriter writer = new FileWriter(batchFile)) {
                writer.write("@echo off\n");
                writer.write("bcdedit /set IncreaseUserVa 3072\n");
                writer.write("pause\n");
            }

            // 创建批处理命令以管理员身份运行
            String command = "cmd /c start cmd /c \"runas /user:Administrator " + batchFile.getAbsolutePath() + "\"";
            Process process = Runtime.getRuntime().exec(command);

            // 等待进程完成
            int exitCode = process.waitFor();
            System.out.println("Exit code: " + exitCode);

            // 结果输出信息到弹窗
            JOptionPane.showMessageDialog(null, "命令执行完成,退出代码: " + exitCode);

            // 删除批处理文件
            if (!batchFile.delete()) {
                JOptionPane.showMessageDialog(null, "无法删除批处理文件: " + batchFile.getAbsolutePath());
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "发生错误: " + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}

相关文章

网友评论

      本文标题:swing

      本文链接:https://www.haomeiwen.com/subject/rmowrjtx.html