基本信息
源码名称:java编写的关机程序
源码大小:0.41M
文件格式:.rar
开发语言:Java
更新时间:2019-10-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

注:运行需要配置jdk环境

新用户求点积分下载资源,还请各位大佬多多支持

 package ui;
 
 import fileoption.FileOption;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.GridLayout;
 import java.awt.Image;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.io.IOException;
 import javax.imageio.ImageIO;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import javax.swing.border.TitledBorder;
 import rbutton.RButton;
 import thread.TimeCountThread;
 
 public class UI extends JFrame
 {
   private static final long serialVersionUID = 1L;
   private Integer time = null;
 
   private JPanel panelChoose = new JPanel(new GridLayout(4, 1));
   private JPanel panelFunc = new JPanel(new GridLayout(2, 1));
   private JPanel countTime = new JPanel();
 
   private static UI ui = new UI();
   private FileOption fileoption = FileOption.initFileOption();
 
   private boolean limit = true;
 
   private int flag = 0;
   private TimeCountThread thread = new TimeCountThread();
 
   public static UI initUI() { return ui; }
 
 
   private UI()
   {
     try
     {
       Image img = ImageIO.read(super.getClass().getResource("/img/1.png"));
       setIconImage(img);
       setIconImage(img); } catch (IOException e) {
       e.printStackTrace();
     }
 
     setAlwaysOnTop(true);
     setTitle("定时关机");
     setSize(300, 200);
     setResizable(false);
     setLocationRelativeTo(null);
     putsetPanel();
     add(this.panelChoose, "West");
     addFunc();
     add(this.panelFunc, "Center");
     setVisible(true);
     setDefaultCloseOperation(3);
   }
 
   private void addBound(String title, JPanel panel, int fontSize)
   {
     TitledBorder titl = new TitledBorder(title);
     titl.setTitleFont(new Font("宋体", 0, fontSize));
     titl.setTitleColor(Color.GRAY);
     panel.setBorder(titl);
   }
 
   private void putsetPanel()
   {
     addBound("请选择", this.panelChoose, 12);
 
     RButton shutDown = new RButton("关       机");
     boundPanel(shutDown);
     shutDown.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e) {
         UI.this.showScreen("即将关机..");
         String content = "shutdown -s -t 0";
         UI.this.fileoption.listenBtn(FileOption.shutdwnFile, content);
         System.exit(0);
       }
     });
     RButton reboot = new RButton("立即重启");
     boundPanel(reboot);
     reboot.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e) {
         UI.this.showScreen("重启中..");
         String content = "shutdown -r -t 0";
         UI.this.fileoption.listenBtn(FileOption.rebootFile, content);
         System.exit(0);
       }
     });
     RButton sleepDown = new RButton("休       眠");
     boundPanel(sleepDown);
     sleepDown.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e) {
         UI.this.showScreen("即将进入休眠状态..");
         String content = "shutdown -h";
         UI.this.fileoption.listenBtn(FileOption.sleepFile, content);
         System.exit(0);
       }
     });
     RButton cancelShut = new RButton("取消关机");
     boundPanel(cancelShut);
     cancelShut.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e) {
         UI.this.thread.setFlagTrue();
 
         if (UI.this.flag == 0) {
           UI.this.showScreen("还没设置关机哟!(?????) ");
         } else {
           UI.this.showScreen("已取消!\\(^o^)/ ");
           String content = "shutdown -a";
           UI.this.fileoption.listenBtn(FileOption.cancelFile, content);
           UI.this.setDefaultCloseOperation(3);
           UI.this.limit = true;
           UI.this.flag = 0;
         }
       }
     });
   }
 
   private void showScreen(String msg)
   {
     this.countTime.removeAll();
     JLabel label = new JLabel(msg);
     label.setFont(getFont());
     this.countTime.add(label);
     this.countTime.validate();
     this.countTime.repaint();
   }
 
   private void boundPanel(RButton button)
   {
     JPanel btn = new JPanel();
     btn.add(button);
     this.panelChoose.add(btn);
   }
 
   private void addFunc()
   {
     this.panelFunc.add(setShutDownTime());
     setCountTime();
     this.panelFunc.add(this.countTime);
   }
 
   private JPanel setShutDownTime()
   {
     JPanel set = new JPanel();
     addBound("定时关机(s)", set, 10);
      JTextField text = new JTextField(4);
     JPanel panel = new JPanel();
     panel.add(text);
     set.add(panel);
     text.addKeyListener(new KeyListener()
     {
       public void keyPressed(KeyEvent e) {
         if (e.getKeyCode() == 10)
           UI.this.packOption(text);
       }
 
       public void keyTyped(KeyEvent e)
       {
       }
 
       public void keyReleased(KeyEvent e)
       {
       }
     });
     JLabel s = new JLabel("秒");
     s.setFont(getFont());
     panel = new JPanel();
     panel.add(s);
     set.add(panel);
 
     RButton queRen = new RButton("确认");
          

       });
     panel = new JPanel();
     panel.add(queRen);
     set.add(panel);
     return set;
   }
 
   private void packOption(JTextField text)
   {
     try
     {
       queRenBtnListen(text);
     }
     catch (Exception e1)
     {
       showScreen(e1.getMessage());
     }
   }
 
   private void queRenBtnListen(JTextField text)
     throws Exception
   {
     if (!this.limit) {
       throw new Exception("请不要重复确认哦!(?ˉ?ˉ?) ");
     }
     if (this.limit) {
       judgeText(text.getText());
       int time = Integer.valueOf(text.getText()).intValue();
       this.time = Integer.valueOf(time);
       setDefaultCloseOperation(0);
       String content = "shutdown -s -t "   time;
       this.fileoption.listenBtn(FileOption.shutdwnFile, content);
 
       this.thread.ui(this);
       if (!this.thread.isAlive()) {
         this.thread.start();
       }
       synchronized (this.thread.syncObj) {
         this.thread.time = this.time;
         this.thread.setFlagFalse();
         this.thread.syncObj.notify();
       }
       this.limit = false;
       this.flag = 1;
     }
   }
 
   private void judgeText(String text)
     throws Exception
   {
     if (("".equals(text.replace(" ", ""))) || (text == null)) {
       throw new Exception("输入不能为空哟!o( ̄ε ̄*) ");
     }
     String regex = "^\\d{1,4}";
     if (!text.matches(regex))
       throw new Exception("请输入4位数以内的数字!(?????) ");
   }
 
   private void setCountTime()
   {
     addBound("提示", this.countTime, 10);
   }
 
   private void showShutDownTime(Integer count)
   {
     JPanel panelTime = new JPanel();
     JLabel label = new JLabel("还有 "   count   " s关机");
     label.setFont(getFont());
     this.countTime.removeAll();
     panelTime.add(label);
     this.countTime.add(panelTime);
     this.countTime.validate();
     this.countTime.repaint();
   }
 
   public void time(Integer count)
   {
     showShutDownTime(count);
   }
 
   public Integer getTime()
   {
     return this.time;
   }
 }