用Java写的关机程序

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class shutdown {
public static void main(String[] args) {
shutdown s = new shutdown();
s.demo();
}

public void demo(){
JFrame jf = new JFrame(“showdown”);

JPanel jp = new JPanel();
jp.setLayout(null);
jf.add(jp);
jf.setLocationRelativeTo(null);
jp.setBounds(0,0,400,400);
JLabel jbt = new JLabel(“关机大法”);
jbt.setFont(new Font(null,Font.BOLD,38));
jbt.setBounds(100, 10, 300, 100);
jp.add(jbt);
JLabel jLabel = new JLabel(“您要多少分钟后关机?请输入,然后点击shutdown按钮”);
jLabel.setForeground(Color.red);
jLabel.setBounds(30, 120, 300, 20);
jp.add(jLabel);
jf.setSize(400,400);
jf.setLayout(null);
final JTextArea jt = new JTextArea();
jt.setBounds(50,150,130,25);
JButton jb = new JButton(“shutdown”);
jb.setBounds(200,150,90,25);
jp.add(jb);
JButton jbre = new JButton(“取消关机”);
jbre.setBounds(200,200,90,25);
jp.add(jt);
jp.add(jbre);
jf.setVisible(true);
jt.repaint();
jb.repaint();
jbre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime r = Runtime.getRuntime();
try {
r.exec(“shutdown -a”);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
String time=jt.getText();
if(time.length()>0)
{
int s = 0;
try{
s = Integer.valueOf(time);
if(s<0){
jt.setText(“请输入正整数”);
return;
}
}
catch(Exception e1){
jt.setText(“请输入正整数”);
return;
}
new myForm(s);
}
}
});
}

}

class myForm extends JFrame{
private int time;

myForm(int time){
this.time=time;
setLocationRelativeTo(null);
setLayout(null);
setSize(300,150);
setVisible(true);
int hour = time/60;
JLabel jl = null;

JButton jbs = new JButton(“确定”);
jbs.setBounds(35, 50, 80, 30);
add(jbs);
jbs.addActionListener(new myevent(true, time,this));
JButton jbr = new JButton(“取消”);
jbr.setBounds(155, 50, 80, 30);
jbr.addActionListener(new myevent(false,time,this));
add(jbr);

if(hour==0){
jl = new JLabel(“系统将在”+time+“分钟后关机!是否确定?”);
jl.setBounds(30, 10, 250, 20);
add(jl);
}
if(hour>=1){
int mimute = time%60;
jl = new JLabel(“系统将在”+hour+“个小时”+mimute+“分钟后关机!是否确定?”);
jl.setBounds(15, 10, 290, 20);
add(jl);
}

}
}

class myevent implements ActionListener{

private int time;
private boolean rs;
private myForm myform;
myevent(boolean rs,int time, myForm myForm){
this.time=time;
this.rs=rs;
this.myform=myForm;
}
public void actionPerformed(ActionEvent e) {
if(rsfalse){
myform.setVisible(false);
}
if(rs
true){
Runtime r = Runtime.getRuntime();
try {
r.exec(“shutdown -s -t “+time*60+” -c 经过tiansir的经密计算,本系统还有”+time+“分钟关机”);
myform.setVisible(false);
} catch (IOException e1) {

e1.printStackTrace();
}
}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值