自动关机,,import java.
分享于 点击 46947 次 点评:79
自动关机,,import java.
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DateFormat;import java.util.Date;import javax.swing.*;public class ShutDown extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JPanel jp1; private JLabel jl1; private JButton jb1; private static String time; private JTextField txt; public ShutDown() { super("自动关机小程序"); setSize(300, 200); getContentPane(); setLocation(400, 200); jb1 = new JButton("确定"); jb1.setLayout(null); jb1.setBounds(new Rectangle(90, 105, 80, 30)); jb1.addActionListener(this); jp1 = new JPanel(); jp1.setLayout(null); jp1.setBounds(new Rectangle(0, 0, 300, 200)); jl1 = new JLabel("请输入时间(例如:17:25:00)"); jl1.setLayout(null); jl1.setBounds(new Rectangle(50, 0, 300, 100)); txt = new JTextField(); txt.setLayout(null); txt.setBounds(new Rectangle(60, 67, 150, 30)); jl1.setVisible(true); jp1.add(jl1, null); jp1.add(txt, null); jp1.add(jb1, null); jp1.setVisible(true); getContentPane().add(jp1, null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent evt) { time = txt.getText(); setVisible(false); } public static void main(String[] args) { new ShutDown(); Runtime run = Runtime.getRuntime(); Process process1 = null; Boolean flag=true; System.out.println(time); while (flag) { Date date = new Date(); DateFormat d1 = DateFormat.getTimeInstance(); String str = d1.format(date); if (str.equals(time)) { try { process1 = run.exec("shutdown /s /t 0"); flag=false; } catch (Exception e1) { System.out.println("Wrong!!"); } try{ Thread.sleep(1000); }catch(Exception e2){} } } }}//该片段来自于http://byrx.net
用户点评