做不出来吧
反正我弄不出来
我只弄了个关机程序
package cn.kylin.huang;
/*
一需求
完成图形化界面实现关机
分析
建立图形化界面(有按键,有设置)
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。
*/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
class CloseTimer
{
private Frame f;
private Button cancel;
private Button close;
private Button restart;
private Button waitclose;
private TextField tf;
CloseTimer()
{
init();
}
public void init()
{
f = new Frame("定时关机for kylin huang");
f.setSize(500,400);//设置窗口尺寸
f.setLocation(300,200);//设置窗口位置
f.setLayout(new FlowLayout());//设置布局为流式布局
close = new Button("关机");//新建一按钮
restart= new Button("重启");//新建一按钮
tf =new TextField(10);//新建单行文本
waitclose= new Button("多少分钟以后关机");//新建一按钮
f.add(close);//按键b添加到窗口f中
f.add(restart);//按键b添加到窗口f中
f.add(tf);
f.add(waitclose);//按键b添加到窗口f中
cancel= new Button("取消");//新建一按钮
f.add(cancel);//按键b添加到窗口f中
myEvent();//关闭事件
f.setVisible(true);//显示隐藏组件
}
private void myEvent()
{
f.addWindowListener (new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//用按钮实现关闭
cancel.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Runtime r= Runtime.getRuntime();
try {
r.exec("cmd /c Shutdown -a");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
cancel.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//添加键盘监听是输入0至9有效
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int code =e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9))
{
System.out.println(code+"input is wrongful");
e.consume();//不做默认处理
}
}
});
//添加鼠标监听实现按下“关机“使其关机
close.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
Runtime r= Runtime.getRuntime();
try {
r.exec("cmd /c Shutdown -s");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//重启
restart.addMouseListener(new MouseAdapter()
{
public void mouseClicked(final MouseEvent e)
{
Runtime r= Runtime.getRuntime();
try {
r.exec("cmd /c Shutdown -r");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//waitclose定时关机
waitclose.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
String time=tf.getText();
System.out.println(time);
Runtime r= Runtime.getRuntime();
try {
r.exec("cmd /c Shutdown -s -t "+time);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String[] args)
{
new CloseTimer();
}
}
|