package AutoShutdown;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
public class AutoShutdownSystem extends JFrame {
private JTextField timeTextField; // 用于输入关机时间的文本框
private JTextField countdownTextField; // 显示倒计时的文本框
private JProgressBar progressBar; // 倒计时进度条
private JButton startButton; // 开始按钮
private JButton stopButton; // 停止按钮
private JButton pauseButton; // 暂停按钮
private JButton resumeButton; // 恢复按钮
private JButton cancelButton; // 取消按钮
private JCheckBox shutdownCheckBox; // 关机复选框
private JCheckBox restartCheckBox; // 重启复选框
private JCheckBox logoutCheckBox; // 注销复选框
private JRadioButton delayRadioButton; // 倒计时模式单选按钮
private JRadioButton timeRadioButton; // 定时模式单选按钮
private ButtonGroup radioButtonGroup; // 单选按钮组
private Timer timer; // 计时器
private int countdown; // 倒计时剩余时间(秒)
private int maxCountdown; // 倒计时总时间(秒)
private boolean isPaused; // 计时器是否被暂停
private boolean isCancelled; // 关机操作是否被取消
private LocalTime shutdownTime; // 定时关机的时间点
public AutoShutdownSystem() {
setTitle("自动关机系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// 创建组件
JLabel timeLabel = new JLabel("关机时间(秒):");
JLabel countdownLabel = new JLabel("倒计时(秒):");
timeTextField = new JTextField(10);
timeTextField.setText("3600");
countdownTextField = new JTextField(10);
countdownTextField.setEditable(false);
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
startButton = new JButton("开始");
stopButton = new JButton("停止");
pauseButton = new JButton("暂停");
resumeButton = new JButton("恢复");
cancelButton = new JButton("取消");
pauseButton.setEnabled(false);
resumeButton.setEnabled(false);
cancelButton.setEnabled(false);
shutdownCheckBox = new JCheckBox("关机");
restartCheckBox = new JCheckBox("重启");
logoutCheckBox = new JCheckBox("注销");
delayRadioButton = new JRadioButton("倒计时模式");
timeRadioButton = new JRadioButton("定时模式");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(delayRadioButton);
radioButtonGroup.add(timeRadioButton);
delayRadioButton.setSelected(true);
// 布局
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
add(timeLabel, gbc);
gbc.gridx++;
add(timeTextField, gbc);
gbc.gridy++;
gbc.gridx = 0;
add(countdownLabel, gbc);
gbc.gridx++;
add(countdownTextField, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(progressBar, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
add(shutdownCheckBox, gbc);
gbc.gridx++;
add(restartCheckBox, gbc);
gbc.gridx++;
add(logoutCheckBox, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
add(delayRadioButton, gbc);
gbc.gridy++;
add(timeRadioButton, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 1;
add(startButton, gbc);
gbc.gridx++;
add(stopButton, gbc);
gbc.gridx++;
add(pauseButton, gbc);
gbc.gridx++;
add(resumeButton, gbc);
gbc.gridx++;
add(cancelButton, gbc);
// 注册事件监听器
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startShutdown();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopShutdown();
}
});
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pauseShutdown();
}
});
resumeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resumeShutdown();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cancelShutdown();
}
});
delayRadioButton.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (delayRadioButton.isSelected()) {
timeTextField.setEnabled(true);
}
}
});
timeRadioButton.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (timeRadioButton.isSelected()) {
timeTextField.setEnabled(false);
}
}
});
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void startShutdown() {
try {
if (delayRadioButton.isSelected()) {
int time = Integer.parseInt(timeTextField.getText());
countdown = time;
maxCountdown = time;
} else {
shutdownTime = LocalTime.parse(timeTextField.getText());
LocalTime currentTime = LocalTime.now();
if (shutdownTime.isBefore(currentTime)) {
JOptionPane.showMessageDialog(this, "无效的关机时间!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
long delaySeconds = TimeUnit.NANOSECONDS.toSeconds(shutdownTime.toNanoOfDay() - currentTime.toNanoOfDay());
countdown = (int) delaySeconds;
maxCountdown = (int) delaySeconds;
}
// 禁用开始按钮,启用停止、暂停和取消按钮
startButton.setEnabled(false);
stopButton.setEnabled(true);
pauseButton.setEnabled(true);
cancelButton.setEnabled(true);
// 创建计时器
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!isPaused) {
countdown--;
countdownTextField.setText(String.valueOf(countdown));
progressBar.setValue((int) (((double) countdown / maxCountdown) * 100));
if (countdown <= 0) {
stopShutdown();
executeShutdownTasks();
} else if (countdown <= 10) {
int choice = JOptionPane.showConfirmDialog(AutoShutdownSystem.this,
"关机倒计时 " + countdown + " 秒,是否停止关机?", "提示", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
stopShutdown();
}
}
}
}
});
// 启动计时器
timer.start();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "请输入一个有效的整数值!", "错误", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "无效的关机时间!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void stopShutdown() {
if (timer != null && timer.isRunning()) {
timer.stop();
}
// 启用开始按钮,禁用停止、暂停和取消按钮
startButton.setEnabled(true);
stopButton.setEnabled(false);
pauseButton.setEnabled(false);
resumeButton.setEnabled(false);
cancelButton.setEnabled(false);
countdownTextField.setText("");
progressBar.setValue(0);
isPaused = false;
isCancelled = false;
}
private void pauseShutdown() {
if (timer != null && timer.isRunning()) {
timer.stop();
isPaused = true;
// 启用恢复按钮,禁用暂停按钮
pauseButton.setEnabled(false);
resumeButton.setEnabled(true);
}
}
private void resumeShutdown() {
if (timer != null && !timer.isRunning()) {
timer.start();
isPaused = false;
// 启用暂停按钮,禁用恢复按钮
pauseButton.setEnabled(true);
resumeButton.setEnabled(false);
}
}
private void cancelShutdown() {
if (timer != null && timer.isRunning()) {
timer.stop();
isCancelled = true;
countdownTextField.setText("");
progressBar.setValue(0);
// 启用开始按钮,禁用停止、暂停和取消按钮
startButton.setEnabled(true);
stopButton.setEnabled(false);
pauseButton.setEnabled(false);
resumeButton.setEnabled(false);
cancelButton.setEnabled(false);
}
}
private void executeShutdownTasks() {
if (!isCancelled) {
if (shutdownCheckBox.isSelected()) {
executeCommand("shutdown -s -t 0");
}
if (restartCheckBox.isSelected()) {
executeCommand("shutdown -r -t 0");
}
if (logoutCheckBox.isSelected()) {
executeCommand("shutdown -l");
}
}
}
private void executeCommand(String command) {
try {
Runtime.getRuntime().exec(command);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "无法执行关机命令!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AutoShutdownSystem();
}
});
}
}
包含图形界面的自动关机系统-课程设计
该程序是一个JavaSwing应用,提供了一个用户界面来设置倒计时或定时关机、重启或注销功能。用户可以输入时间,选择操作类型,并通过开始、停止、暂停、恢复和取消按钮控制关机过程。程序还包括了对用户输入的验证和异常处理。
摘要由CSDN通过智能技术生成