文章目录
- 程序说明
- 运行截图
- 项目结构截图
- main类
- ActionListener1类
- ItemListener1类
- Jfwindow类
- shut类
- XialaItmeListener类
- module-info.java
- 左上角的图标
- 总结
程序说明 本程序是利用Java的GUI编写的通过执行cmd命令来进行的简单的定时关机和定时重启的程序,本程序具有定时关机与定时重启的功能可以按小时定时,也可以按分钟定时以及按秒来定时,最大的定时时间为315360000秒 。使用了maven进行构建jdk11进行开发工具使用的是ideaj社区版,项目的地址为定时关机
运行截图
项目结构截图
main类
/* 作者:徐俊日、 *版本:2.0*/package shutdown;public class main { public static void main(String atgs[]) {Jfwindow jfwindow=new Jfwindow();jfwindow.ShowWindow();//显示窗口布局 }} ActionListener1类 package shutdown;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;public class ActionListener1 implements ActionListener{ Jfwindow view;//设置jfwindow的变量view int str;static String jrbut="shutdown";//类变量共用储存public void setView(Jfwindow view) {this.view=view; } //调用JOptionPane类的静态方法showmessagedialog方法来创建一个消息对话框 //第一个参数指定对话框可见时的位置,null在屏幕正前方显示,不为空在正前面居中显示; //第二个参数设置要显示的消息 //第三个参数设置对话框的标题 //第四个参数指定对话框的类型,例如warning_message信息右侧会显示一个!号 //第五个参数an icon to display in the dialog that helps the useridentify the kind of message that is being displayed public void actionPerformed(ActionEvent e) {String onlynum="^[0-9]+$";//正则表达式来判断输入的是不是正整数String str=view.text1.getText();int time;shut cancel=new shut();shut shut=new shut();if(e.getSource()==view.button1)//当选中第一个单选按钮和点击第一个按钮后的操作{if(str.matches(onlynum)) //Matched()方法用来对滴str与onlynum是不是符合{time=Integer.parseInt(str);//将文本型数字转换成int型数字if(XialaItmeListener.item=="秒"){if(time>=0 && time<315360000){System.out.print(time+"秒\n");//在控制台输出内容shut.getTime(time);//符合则将时间传入给shut类if(jrbut=="shutdown") //通过jrbut的值来判断选择哪个功能{shut.shutdown();}else if(jrbut=="reboot"){shut.Reboot();}else if(jrbut==null){JOptionPane.showMessageDialog(view, "请选择功能!", "警告",JOptionPane.INFORMATION_MESSAGE);}}else{JOptionPane.showMessageDialog(view, time+"超出最大时间315360000秒", "警告",JOptionPane.WARNING_MESSAGE);}}else if(XialaItmeListener.item=="分钟"){time=time*60;if(time>=0 && time<315360000){System.out.print(time/60+"分钟\n");//在控制台输出内容shut.getTime(time);//符合则将时间传入给shut类if(jrbut=="shutdown") //通过jrbut的值来判断选择哪个功能{shut.shutdown();}else if(jrbut=="reboot"){shut.Reboot();}else if(jrbut==null){JOptionPane.showMessageDialog(view, "请选择功能!", "警告",JOptionPane.INFORMATION_MESSAGE);}}else{JOptionPane.showMessageDialog(view, time/60+"超出最大时间5256000分钟", "警告",JOptionPane.WARNING_MESSAGE);}}else if(XialaItmeListener.item=="小时"){time=time*60*60;if(time>=0 && time<315360000){System.out.print(time/60/60+"小时\n");//在控制台输出内容shut.getTime(time);//符合则将时间传入给shut类if(jrbut=="shutdown") //通过jrbut的值来判断选择哪个功能{shut.shutdown();}else if(jrbut=="reboot"){shut.Reboot();}else if(jrbut==null){JOptionPane.showMessageDialog(view, "请选择功能!", "警告",JOptionPane.INFORMATION_MESSAGE);}}else{JOptionPane.showMessageDialog(view, time/60/60+"超出最大时间87600小时", "警告",JOptionPane.WARNING_MESSAGE);}}}else{JOptionPane.showMessageDialog(view, "请输入正整数时间", "定时关机", JOptionPane.WARNING_MESSAGE, null);}}else if(e.getSource()==view.button2){cancel.Cancel();} }} ItemListener1类 package shutdown;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;public class ItemListener1 implements ItemListener//为单选按钮添加不同的监听器来监听悬着的功能{Jfwindow view2;//设置jfwindow的变量view2,用来储存jfwindow对象的引用 public void setView(Jfwindow view) {this.view2=view; }@Override public void itemStateChanged(ItemEvent e){// TODO 自动生成的方法存根if(e.getSource()==view2.jrbutton1){ActionListener1.jrbut="shutdown";//调用actionlistener的类变量来改变jrbut变量的值}else if(e.getSource()==view2.jrbutton2){ActionListener1.jrbut="reboot";} } } Jfwindow类 package shutdown;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.Image;import java.awt.Toolkit;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JTextField;/*先设置窗口,然后设置容器例如:Panel、JTabbedPane、JScrollPane、JSplitPane等容器 * 然后为容器设置布局例如:网格布局gridlayout、flowlayout等 * 然后设置组件,最后添加组件完成窗口设置,* 如果想要添加多个容器到窗口中,那么先要设置窗口的布局例如:flowlayout等,* 然后再添加其他的容器,如果不为窗口设置布局那么只能添加一个容器 */@SuppressWarnings("serial")public class Jfwindow extends JFrame{ JLabel Label1,Label2; JButton button1,button2; ButtonGroup buttongroup; JRadioButton jrbutton1,jrbutton2; JTextField text1; JComboBox combox;JPanel jpanel1; JPanel jpanel2; JPanel jpanel3; JPanel jpanel4; ActionListener1 listener; ItemListener1 listener2; XialaItmeListener listener3; public void ShowWindow()//设置窗口 {JFrame jframe=new JFrame();//添加一个窗口对象GridLayout gridlayout=new GridLayout(4,1,0,0);//设置2行1列的网格布局,四个参数分别是行数、水平间距、垂直间距jframe.setLayout(gridlayout);//为frame添加布局才能添加多个容器,否者只能添加一个容器Setlayout();jframe.add(jpanel1);//添加容器jframe.add("这是面板2",jpanel2);//两种不同的add容器方法效果是一样的jframe.add(jpanel3);jframe.add(jpanel4);jframe.setSize(400,300);//设置窗口大小jframe.setLocationRelativeTo(null);//设置窗口居中显示jframe.setTitle("定时关机2.0");Image icon=Toolkit.getDefaultToolkit().getImage("src/main/resources/image/dingshi.png"); //修改左上角的默认图标jframe.setIconImage(icon);jframe.setResizable(false);//让窗口大小不可调jframe.setVisible(true);jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void Setlayout()//设置容器 {Font font=new Font("宋体",Font.BOLD,25);//设置字体Font font2=new Font("黑体",Font.HANGING_BASELINE,15);jpanel1=new JPanel();//设置面板容器,可以设置多个容器jpanel2=new JPanel();jpanel3=new JPanel();jpanel4=new JPanel();FlowLayout fllayout1=new FlowLayout(FlowLayout.LEFT);//设置flowlayout布局方式为LEFT对齐FlowLayout fllayout2=new FlowLayout();FlowLayout fllayout3=new FlowLayout(FlowLayout.CENTER);FlowLayout fllayout4=new FlowLayout();jpanel1.setLayout(fllayout1);//设置面板容器的布局jpanel2.setLayout(fllayout2);jpanel3.setLayout(fllayout3);jpanel4.setLayout(fllayout4);jrbutton1=new JRadioButton("关机",true);//设置组件的参数,false代表未选中jrbutton2=new JRadioButton("重启");buttongroup=new ButtonGroup();//为单选按钮设置按钮组,否者无法实现单选buttongroup.add(jrbutton1);//归组才能实现单选buttongroup.add(jrbutton2);button1=new JButton("开始");button2=new JButton("取消");Label1=new JLabel("请输入时间:");Label2=new JLabel("作者邮箱:hmyxhxjr@qq.com");text1=new JTextField(null,null,10);combox=new JComboBox();//创建下拉列表combox.addItem("秒");//为下拉列表添加选项combox.addItem("分钟");combox.addItem("小时");Label1.setFont(font);//设置字体和大小Label2.setFont(font2);jrbutton1.setFont(font);jrbutton2.setFont(font);text1.setFont(font);button1.setFont(font);button2.setFont(font);combox.setFont(font);jpanel3.add(jrbutton1);/*为容器添加组件*/jpanel3.add(jrbutton2);jpanel1.add(Label1);jpanel2.add(text1);jpanel4.add(button1);//不同的容器可以添加不同的组件jpanel4.add(button2);jpanel2.add(combox);listener=new ActionListener1();//创建监视器listener2=new ItemListener1();listener3=new XialaItmeListener();listener.setView(this);//将当前窗口传递给listener组合的窗口listener2.setView(this);listener3.setView(this);jrbutton1.addItemListener(listener2);//jrbutton将listener注册为自己的监听器jrbutton2.addItemListener(listener2);button1.addActionListener(listener);button2.addActionListener(listener);text1.addActionListener(listener);combox.addItemListener(listener3); } } shut类 package shutdown;import java.io.IOException;import javax.swing.JOptionPane;public class shut { Jfwindow view; int time; public int getTime(int time) {this.time=time;return time; }void shutdown()//关机 {String str2="shutdown -s -t "+time;try {Runtime.getRuntime().exec(str2);//使用该方法来运行DOS的关机命令System.out.println(str2);JOptionPane.showMessageDialog(view, "已经开时定时关机", "定时关机", JOptionPane.OK_CANCEL_OPTION, null);} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}} void Reboot()//重启 {String str2="shutdown -r -t "+time;try {Runtime.getRuntime().exec(str2);System.out.println(str2);JOptionPane.showMessageDialog(view,"已经开始定时重启", "定时重启", JOptionPane.OK_CANCEL_OPTION, null);} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}} void Cancel() //取消 {try {Runtime.getRuntime().exec("shutdown -a");System.out.println("shutdown -a");JOptionPane.showMessageDialog(view, "已经取消", "取消定时任务", JOptionPane.OK_OPTION, null);} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();} }} XialaItmeListener类 package shutdown;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;public class XialaItmeListener implements ItemListener{ Jfwindow view; public static String item="秒";public void setView(Jfwindow view) {this.view=view; } @Override public void itemStateChanged(ItemEvent e) {// TODO 自动生成的方法存根item=view.combox.getSelectedItem().toString();}} module-info.java module dingshiguanji { requires java.desktop;} 左上角的图标总结 【利用Java的GUI编写的定时关机程序】module-info.java这个Java文件一定要加不加的话就报错 。
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
