定时启动软件问题
想通过java编写一个用户界面,通过设置具体的时间,来运行文件选择器里选定的文件,可是始终无法实现某些功能,现在有几个疑问:如何动态的获取系统的时间,以及如何真正的实现系统时间如自己设定时间的比较,还有就是如何在设定的时间运行选择的exe文件,基本代码在下面,麻烦大家看看package ch10;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.text.*;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.util.Date;
import java.util.Calendar;
public class Timing extends JFrame implements ActionListener {
JPanel jp = new JPanel();
JButton jb1 = new JButton("浏览");
JButton jb2 = new JButton("确定");
JLabel jl1 = new JLabel("运行时间");
JLabel jl2 = new JLabel("时");
JLabel jl3 = new JLabel("分");
JLabel jl4 = new JLabel("秒");
JLabel jl[] = new JLabel[] { jl1, jl2, jl3, jl4 };
JTextArea jta1 = new JTextArea();
JTextArea jta2 = new JTextArea();
JTextArea jta3 = new JTextArea();
JTextArea jta4 = new JTextArea();
JTextArea jta[] = new JTextArea[] { jta1, jta2, jta3, jta4 };
JFileChooser fileChooser = new JFileChooser("E:\\");
public Timing() {
// TODO Auto-generated constructor stub
jp.setLayout(null);
jp.add(jb1);
jp.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
for (int i = 0; i < jl.length; i++) {
jp.add(jl[i]);
jp.add(jta[i]);
jta[i].setEditable(true);
}
jl1.setBounds(10, 10, 60, 20);
jta1.setBounds(80, 10, 40, 20);
jl2.setBounds(130, 10, 20, 20);
jta2.setBounds(160, 10, 40, 20);
jl3.setBounds(210, 10, 20, 20);
jta3.setBounds(240, 10, 40, 20);
jl4.setBounds(290, 10, 20, 20);
jta4.setBounds(10, 40, 300, 20);
jb1.setBounds(320, 40, 80, 20);
jb2.setBounds(10, 70, 80, 20);
fileChooser.removeChoosableFileFilter(fileChooser
.getChoosableFileFilters()[0]);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(".exe",
"exe"));
this.add(jp);
this.setTitle("定时启动器");
this.setBounds(110, 110, 500, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
fileChooser.showOpenDialog(this);
String path = fileChooser.getSelectedFile().getPath().toString();
// path.replace("\\","");
jta4.setText(path);
}
if (e.getSource() == jb2) {
if (jta1.getText().equals("") || jta2.getText().equals("")
|| jta3.getText().equals("") || jta4.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整的信息", "错误",
JOptionPane.INFORMATION_MESSAGE);
} else {
while (true) {
Calendar cal = Calendar.getInstance();
if (cal.HOUR_OF_DAY == Integer.parseInt(jta1.getText())
&& cal.MINUTE == Integer.parseInt(jta2.getText())) {
String strCommand = fileChooser.getSelectedFile()
.getPath().toString();
try {
Runtime.getRuntime().exec(strCommand);
}
catch (IOException ex) {
ex.printStackTrace();
}
break;
}
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Timing();
}
}
----------------解决方案--------------------------------------------------------
不懂
----------------解决方案--------------------------------------------------------
程序进入死循环了, 当前时间只能创建一次实例, 所以cal.HOUR和cal.MINUTE是没有变化的. while循环也就不能break
时间控制,改用线程是否好些?
[ 本帖最后由 lampeter123 于 2011-1-11 09:38 编辑 ]
----------------解决方案--------------------------------------------------------
帮你改用Timer类 和 TimerTask类
程序代码:
package ch10;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.text.*;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.util.Date;
import java.util.Calendar;
import java.util.Timer;
public class Timing extends JFrame implements ActionListener {
JPanel jp = new JPanel();
JButton jb1 = new JButton("浏览");
JButton jb2 = new JButton("确定");
JLabel jl1 = new JLabel("运行时间");
JLabel jl2 = new JLabel("时");
JLabel jl3 = new JLabel("分");
JLabel jl4 = new JLabel("秒");
JLabel jl[] = new JLabel[] { jl1, jl2, jl3, jl4 };
JTextArea jta1 = new JTextArea();
JTextArea jta2 = new JTextArea();
JTextArea jta3 = new JTextArea();
JTextArea jta4 = new JTextArea();
JTextArea jta[] = new JTextArea[] { jta1, jta2, jta3, jta4 };
JFileChooser fileChooser = new JFileChooser("E:\\");
public Timing() {
// TODO Auto-generated constructor stub
jp.setLayout(null);
jp.add(jb1);
jp.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
for (int i = 0; i < jl.length; i++) {
jp.add(jl[i]);
jp.add(jta[i]);
jta[i].setEditable(true);
}
jl1.setBounds(10, 10, 60, 20);
jta1.setBounds(80, 10, 40, 20);
jl2.setBounds(130, 10, 20, 20);
jta2.setBounds(160, 10, 40, 20);
jl3.setBounds(210, 10, 20, 20);
jta3.setBounds(240, 10, 40, 20);
jl4.setBounds(290, 10, 20, 20);
jta4.setBounds(10, 40, 300, 20);
jb1.setBounds(320, 40, 80, 20);
jb2.setBounds(10, 70, 80, 20);
fileChooser.removeChoosableFileFilter(fileChooser
.getChoosableFileFilters()[0]);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(".exe",
"exe"));
this.add(jp);
this.setTitle("定时启动器");
this.setBounds(110, 110, 500, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
fileChooser.showOpenDialog(this);
String path = fileChooser.getSelectedFile().getPath().toString();
// path.replace("\\","");
jta4.setText(path);
}
if (e.getSource() == jb2) {
if (jta1.getText().equals("") || jta2.getText().equals("")
|| jta3.getText().equals("") || jta4.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整的信息", "错误",
JOptionPane.INFORMATION_MESSAGE);
} else {
String strCommand = fileChooser.getSelectedFile()
.getPath().toString();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(jta1.getText()));
calendar.set(Calendar.MINUTE, Integer.parseInt(jta2.getText()));
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();
Timer timer = new Timer();
timer.schedule(new MyTimerTask(strCommand), time);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Timing();
}
}
class MyTimerTask extends TimerTask
{
String str = null;
public MyTimerTask(String str)
{
this.str = str;
}
@Override
public void run() {
try {
Runtime.getRuntime().exec(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.text.*;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.util.Date;
import java.util.Calendar;
import java.util.Timer;
public class Timing extends JFrame implements ActionListener {
JPanel jp = new JPanel();
JButton jb1 = new JButton("浏览");
JButton jb2 = new JButton("确定");
JLabel jl1 = new JLabel("运行时间");
JLabel jl2 = new JLabel("时");
JLabel jl3 = new JLabel("分");
JLabel jl4 = new JLabel("秒");
JLabel jl[] = new JLabel[] { jl1, jl2, jl3, jl4 };
JTextArea jta1 = new JTextArea();
JTextArea jta2 = new JTextArea();
JTextArea jta3 = new JTextArea();
JTextArea jta4 = new JTextArea();
JTextArea jta[] = new JTextArea[] { jta1, jta2, jta3, jta4 };
JFileChooser fileChooser = new JFileChooser("E:\\");
public Timing() {
// TODO Auto-generated constructor stub
jp.setLayout(null);
jp.add(jb1);
jp.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
for (int i = 0; i < jl.length; i++) {
jp.add(jl[i]);
jp.add(jta[i]);
jta[i].setEditable(true);
}
jl1.setBounds(10, 10, 60, 20);
jta1.setBounds(80, 10, 40, 20);
jl2.setBounds(130, 10, 20, 20);
jta2.setBounds(160, 10, 40, 20);
jl3.setBounds(210, 10, 20, 20);
jta3.setBounds(240, 10, 40, 20);
jl4.setBounds(290, 10, 20, 20);
jta4.setBounds(10, 40, 300, 20);
jb1.setBounds(320, 40, 80, 20);
jb2.setBounds(10, 70, 80, 20);
fileChooser.removeChoosableFileFilter(fileChooser
.getChoosableFileFilters()[0]);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(".exe",
"exe"));
this.add(jp);
this.setTitle("定时启动器");
this.setBounds(110, 110, 500, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
fileChooser.showOpenDialog(this);
String path = fileChooser.getSelectedFile().getPath().toString();
// path.replace("\\","");
jta4.setText(path);
}
if (e.getSource() == jb2) {
if (jta1.getText().equals("") || jta2.getText().equals("")
|| jta3.getText().equals("") || jta4.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整的信息", "错误",
JOptionPane.INFORMATION_MESSAGE);
} else {
String strCommand = fileChooser.getSelectedFile()
.getPath().toString();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(jta1.getText()));
calendar.set(Calendar.MINUTE, Integer.parseInt(jta2.getText()));
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();
Timer timer = new Timer();
timer.schedule(new MyTimerTask(strCommand), time);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Timing();
}
}
class MyTimerTask extends TimerTask
{
String str = null;
public MyTimerTask(String str)
{
this.str = str;
}
@Override
public void run() {
try {
Runtime.getRuntime().exec(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
----------------解决方案--------------------------------------------------------