该程序实现关机与重启操作。程序编译正常,但是Java程序运行后再次点击按钮会重复弹窗,每多一次按按钮弹窗越多。
import com.fjl.uc.MyJframe;
/**
* @author FJL
* @version 1.0
*/
public class app {
public static void main(String[] args) {
new MyJframe();
}
}
package com.fjl.uc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class MyJframe extends JFrame implements ActionListener {
JButton shotdown1= new JButton("关机");
JButton restart1 = new JButton("重启");
JButton shotdown2 = new JButton("定时关机1h");
JButton restart2 = new JButton("定时重启1h");
JButton cancel = new JButton("取消操作");
boolean flag = false;
public MyJframe(){
initJFrame();
initView();
//显示
this.setVisible(true);
}
private void initView() {
this.getContentPane().removeAll();
if(flag == true){
cancel.setBounds(50,20,100,30);
cancel.addActionListener(this);
this.getContentPane().add(cancel);
}
JLabel text = new JLabel("关机程序");
text.setFont(new Font("微软雅黑",0,30));
text.setBounds(190,80,300,50);
shotdown1.setBounds(200,150,100,30);
restart1.setBounds(200,250,100,30);
shotdown2.setBounds(200,350,100,30);
restart2.setBounds(200,450,100,30);
shotdown1.addActionListener(this);
restart1.addActionListener(this);
shotdown2.addActionListener(this);
restart2.addActionListener(this);
this.getContentPane().add(text);
this.getContentPane().add(shotdown1);
this.getContentPane().add(restart1);
this.getContentPane().add(shotdown2);
this.getContentPane().add(restart2);
this.getContentPane().repaint();
}
private void initJFrame() {
//设置宽高
this.setSize(500,600);
//设置标题
this.setTitle("关机程序");
//设置关闭模式
this.setDefaultCloseOperation(3);
//置顶
this.setAlwaysOnTop(true);
//居中
this.setLocationRelativeTo(null);
//取消内部默认布局
this.setLayout(null);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if(obj == shotdown1){
//关机
flag = true;
showJDialog("计算机将立即关机");
try {
Runtime.getRuntime().exec("shutdown -s -t 0");
} catch (IOException ioException) {
ioException.printStackTrace();
}
initView();
}else if(obj == restart1){
//重启
flag = true;
showJDialog("计算机将立即重启");
try {
Runtime.getRuntime().exec("shutdown -r -t 0");
} catch (IOException ioException) {
ioException.printStackTrace();
}
initView();
}else if(obj == shotdown2){
//定时关机
flag = true;
showJDialog("计算机将于1hour后关机");
try {
Runtime.getRuntime().exec("shutdown -s -t 3600");
} catch (IOException ioException) {
ioException.printStackTrace();
}
initView();
}else if(obj == restart2){
//定时重启
flag = true;
showJDialog("计算机将于1hour后重启");
try {
Runtime.getRuntime().exec("shutdown -r -t 3600");
} catch (IOException ioException) {
ioException.printStackTrace();
}
initView();
}else if(obj == cancel){
//取消关机操作
flag = false;
showJDialog("操作已取消");
try {
Runtime.getRuntime().exec("shutdown -a");
} catch (IOException ioException) {
ioException.printStackTrace();
}
initView();
}
}
public void showJDialog(String content) {
//创建一个弹框对象
JDialog jDialog = new JDialog();
//给弹框设置大小
jDialog.setSize(200, 150);
//让弹框置顶
jDialog.setAlwaysOnTop(true);
//让弹框居中
jDialog.setLocationRelativeTo(null);
//弹框不关闭永远无法操作下面的界面
jDialog.setModal(true);
//关闭模式
jDialog.setDefaultCloseOperation(2);
//创建Jlabel对象管理文字并添加到弹框当中
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 200, 150);
jDialog.getContentPane().add(warning);
//让弹框展示出来
jDialog.setVisible(true);
}
}