关机工具(Timer类+系统命令)

主要实现计算机倒计时关机。定时关机。取消关机。

类CloseComputer:import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class CloseComputer extends JFrame implements ActionListener{
private JPanel panel_main = new JPanel( new BorderLayout(5, 10) );
private JPanel panel_subnorth = new JPanel( new FlowLayout(3) );
private JPanel panel_subcenter = new JPanel( new FlowLayout(1, 5, 5) );

private JButton countdown = new JButton("倒计时关机");
private JButton time = new JButton("定时关机");
private JButton cancel = new JButton("取消关机");

private JLabel tag;
String key;;

public CloseComputer(){
this.add(panel_main);

panel_main.add(panel_subnorth, BorderLayout.NORTH);
panel_main.add(panel_subcenter, BorderLayout.CENTER);

panel_subnorth.add(tag = new JLabel("请选择关机方式:"));

panel_subcenter.add(countdown);
panel_subcenter.add(time);
panel_subcenter.add(cancel);

countdown.addActionListener(this);
time.addActionListener(this);
cancel.addActionListener(this);

}

public static void main(String args[]) throws Exception{
CloseComputer frame = new CloseComputer();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(320,120); //??
frame.setTitle("关机工具");
frame.setLocation(350, 350); //将组建移到新位子

frame.setVisible(true);
frame.setResizable(false);

}

public void countdown(){ //倒计时关机调用的方法
key = JOptionPane.showInputDialog(this, "请输入倒计时关机剩余的时间(秒)", "输入框", 1);
CountTimeTool.delaytime(Long.parseLong(key));
}

public void time(){
Calendar calendar = Calendar.getInstance(); //获取当前系统的时间
//获取当前的时分秒
int h = calendar.get(Calendar.HOUR);
int m = calendar.get(Calendar.MINUTE);
int s = calendar.get(Calendar.SECOND);

int hour, minute, second;
String hourtmp, minutetmp, secondtmp;

hourtmp = JOptionPane.showInputDialog(this, "请输入关机的小时(12小时制)", "输入", 1);
minutetmp = JOptionPane.showInputDialog(this, "请输入关机的分钟", "输入", 1);
secondtmp = JOptionPane.showInputDialog(this, "请输入关机的秒钟", "输入", 1);

hour = Integer.parseInt(hourtmp);
minute = Integer.parseInt(minutetmp);
second = Integer.parseInt(secondtmp);

long set_time = timesum(hour, minute, second);
long currently_time = timesum(h, m, s);

long discrepancy_time = set_time - currently_time;
if(discrepancy_time < 0){
//执行关闭功能
try{
Runtime.getRuntime().exec("shutdown -s");
}catch(IOException e){
e.printStackTrace();
}

}else{
CountTimeTool.delaytime(discrepancy_time);
JOptionPane.showMessageDialog(this, "恭喜你设置成功", "确认", 2);
}
}

public int timesum(int h, int m, int s){
int sum = h * 3600 + m * 60 + s;
return sum;
}

public void cancel(){
try{
JOptionPane.showMessageDialog(this, "你已经成功取消了关机操作", "消息", 2);
Runtime.getRuntime().exec("shutdown -a");
}catch(IOException e){
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent eAction){
String ActionCommand = eAction.getActionCommand();
if(eAction.getSource() instanceof JButton){

if("倒计时关机".equals(ActionCommand))
countdown();
if("定时关机".equals(ActionCommand))
time();
if("取消关机".equals(ActionCommand))
cancel();
}
}
}  类:CountTimeTool: import java.util.Timer;


public class CountTimeTool {
public static void delaytime(long dt){
long delay = 1000;
Timer timer = new Timer();
CutDownTool wl = new CutDownTool();
timer.schedule(wl, delay * dt);

}

}  类CutDownTool:  import java.io.IOException;
import java.util.TimerTask;


public class CutDownTool extends TimerTask{
public void run(){
try{
Runtime.getRuntime().exec("shutdown -s");
}catch(IOException e){
e.printStackTrace();
}
}

} 无聊的结果: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值