实例介绍
【实例简介】
注:运行需要配置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; } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论