当前位置: 代码迷 >> Eclipse >> 新手 提问 关于用 eclipse 中 写代码的有关问题
  详细解决方案

新手 提问 关于用 eclipse 中 写代码的有关问题

热度:495   发布时间:2016-04-23 14:17:17.0
新手 提问 关于用 eclipse 中 写代码的问题?
我是新手 我想请教下 高手 在eclipse环境中 写一个 关于电脑自动关机的 程序 ? 
又哪个高手 可以帮帮忙吗? 我 还写不出来。谢谢了

------解决方案--------------------
//package mm;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class shutdown extends JFrame implements ActionListener{
JButton b1=new JButton("自动关机");
JButton b2=new JButton("取消关机");
JButton b3=new JButton("定时关机");
JButton b4=new JButton("重新启动");
public shutdown()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,2));
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
this.getContentPane().add(p);
b1.setActionCommand("shutdown");
b2.setActionCommand("cancel");
b3.setActionCommand("Time");
b4.setActionCommand("restart");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
this.setSize(200, 110);
this.setLocation(300, 300);
this.setVisible(true);
}



public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if(s.equals("shutdown"))//----自动关机
try
{
Runtime.getRuntime().exec("shutdown -s -f -t 100 -c 请保存相关内容,系统将在100秒后强制关机");
}catch(IOException e1){}
else if(s.equals("cancel"))//----取消关机
try
{
Runtime.getRuntime().exec("shutdown -a");
}catch(IOException e2){}

else if(s.equals("Time"))//----定时关机
try
{
Runtime.getRuntime().exec("Shutdown.exe -s -t 1800 -c 请保存相关内容,系统将在半小时后强制关机");
}catch(IOException e2){}

else //----重新启动计算机
try
{
Runtime.getRuntime().exec("shutdown -r -t 100 -c 请保存相关内容,系统将在100秒后关闭并重启");
}catch(IOException e2){}


}

public static void main(String args[])
{
new shutdown();
}
}
  相关解决方案