java基于GUI的定时关机程序
2016-09-23 17:45
337 查看
思路:
利用java调用本地的dos命令,控制定时关机任务,并通过简单的GUI界面设置时长,控制任务进行,取消。代码可直接复制后运行。。
public class Helper extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new Helper();
}
String time = null;
JLabel label2;
static int totalTime = 0;
public Helper() {
JLabel jl = new JLabel("欢迎使用",SwingUtilities.CENTER);
Font font = new Font("宋体",Font.BOLD,24);
jl.setFont(font);
jl.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
this.add(jl,BorderLayout.NORTH);
Font font1 = new Font("宋体",Font.BOLD,12);
label2 = new JLabel("定时关机",SwingUtilities.CENTER);
label2.setFont(font1);
label2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
this.add(label2,BorderLayout.CENTER);
JButton jb1 = new JButton("取消");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String[] cmd = new String[]{"cmd.exe","/c","shutdown -a"};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "取消成功!");
dispose();
}
});
JButton jb2 = new JButton("确定");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String total = JOptionPane.showInputDialog("请输入定时关机时长!(秒)");
totalTime = Integer.parseInt(total);
String[] cmd = new String[]{"cmd.exe","/c","shutdown -s -t "+totalTime};
try {
Runtime.getRuntime().exec(cmd);
Helper helper = new Helper();
new Thread(helper).start();
helper.setVisible(true);
JOptionPane.showMessageDialog(null, "定时成功!");
dispose();
helper.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JPanel jp_south = new JPanel();
jp_south.add(jb2);
jp_south.add(jb1);
jp_south.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.add(jp_south,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(200, 300);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public void run() {
while (true) {
label2.setText("关机倒计时:"+totalTime+"秒");
try {
Thread.sleep(1000);
totalTime = totalTime - 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
利用java调用本地的dos命令,控制定时关机任务,并通过简单的GUI界面设置时长,控制任务进行,取消。代码可直接复制后运行。。
public class Helper extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new Helper();
}
String time = null;
JLabel label2;
static int totalTime = 0;
public Helper() {
JLabel jl = new JLabel("欢迎使用",SwingUtilities.CENTER);
Font font = new Font("宋体",Font.BOLD,24);
jl.setFont(font);
jl.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
this.add(jl,BorderLayout.NORTH);
Font font1 = new Font("宋体",Font.BOLD,12);
label2 = new JLabel("定时关机",SwingUtilities.CENTER);
label2.setFont(font1);
label2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
this.add(label2,BorderLayout.CENTER);
JButton jb1 = new JButton("取消");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String[] cmd = new String[]{"cmd.exe","/c","shutdown -a"};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "取消成功!");
dispose();
}
});
JButton jb2 = new JButton("确定");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String total = JOptionPane.showInputDialog("请输入定时关机时长!(秒)");
totalTime = Integer.parseInt(total);
String[] cmd = new String[]{"cmd.exe","/c","shutdown -s -t "+totalTime};
try {
Runtime.getRuntime().exec(cmd);
Helper helper = new Helper();
new Thread(helper).start();
helper.setVisible(true);
JOptionPane.showMessageDialog(null, "定时成功!");
dispose();
helper.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JPanel jp_south = new JPanel();
jp_south.add(jb2);
jp_south.add(jb1);
jp_south.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.add(jp_south,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(200, 300);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public void run() {
while (true) {
label2.setText("关机倒计时:"+totalTime+"秒");
try {
Thread.sleep(1000);
totalTime = totalTime - 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- Windows XP自带的定时关机功能以及我的基于此的小程序
- 用java写的基于Socket的简单即时通讯程序
- Java写的GUI程序在Gnome下显示中文乱码,不显示菜单等问题
- bat的定时关机程序
- JAVA综合(系统信息,定时关机,计算器,日历,通讯录)
- 一个java关机程序
- 一个定时关机的小程序
- 一个在windows和基于SWT的java程序间拖拽的例子
- 用于 Java 程序(J2SE, JSP/Servlet) 的可直接返回连接以及定时关闭超时连接的连接池程序
- 基于Java 开发QuickTime 程序
- linux下使用cron每天定时执行java程序
- 向你的Java程序加入开放性资源、基于XML的即时消息
- 使用Java基础定时执行程序
- Java定时重复执行程序
- 想不到Java也能让我基于TCP/IP编写程序,还是如此简单!
- 在linux中定时调用Java程序
- 基于Java语言的51单片机串口通讯PC机程序
- 如何设计出具有GUI特色的“纯”JAVA程序
- Java定时重复执行程序
- Jcrontab - java定时程序进阶学习-Java基础-Java-编程开发