在两位版主大人的帮助下,终于完成了定时启动器了
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
import *;
import java.util.*;
import java.text.*;
import
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 {
boolean task1 = true, task2 = true;
JPanel jp = new JPanel();
JButton jb1 = new JButton("浏览");
JButton jb2 = new JButton("确定");
JButton jb3 = new JButton("浏览");
JLabel jl1 = new JLabel("运行时间");
JLabel jl2 = new JLabel("时");
JLabel jl3 = new JLabel("分");
JLabel jl4 = new JLabel("秒");
JLabel jl5 = new JLabel("运行时间");
JLabel jl6 = new JLabel("时");
JLabel jl7 = new JLabel("分");
JLabel jl8 = new JLabel("秒");
JLabel jl[] = new JLabel[] { jl1, jl2, jl3, jl4, jl5, jl6, jl7, jl8 };
JTextArea jta1 = new JTextArea();
JTextArea jta2 = new JTextArea();
JTextArea jta3 = new JTextArea();
JTextArea jta4 = new JTextArea();
JTextArea jta5 = new JTextArea();
JTextArea jta6 = new JTextArea();
JTextArea jta7 = new JTextArea();
JTextArea jta8 = new JTextArea();
JTextArea jta[] = new JTextArea[] { jta1, jta2, jta3, jta4, jta5, jta6,
jta7, jta8 };
JFileChooser fileChooser = new JFileChooser("E:\\");
public class alarm extends Thread {
public void run() {
while (task1 == true || task2 == true) {
Calendar cal = Calendar.getInstance();
if (task1==true&&(cal.get(Calendar.HOUR_OF_DAY) == Integer.valueOf(
jta1.getText().toString()).intValue()
&& cal.get(Calendar.MINUTE) == Integer.valueOf(
jta2.getText().toString()).intValue()
&& cal.get(Calendar.SECOND) == Integer.valueOf(
jta3.getText().toString()).intValue())) {
String strCommand = jta4.getText().toString();
try {
task1 = false;
Thread.sleep(1000);
Runtime.getRuntime().exec(strCommand);
} catch (Exception ex) {
}
}
if (task2==true&&(cal.get(Calendar.HOUR_OF_DAY) == Integer.valueOf(
jta5.getText().toString()).intValue()
&& cal.get(Calendar.MINUTE) == Integer.valueOf(
jta6.getText().toString()).intValue()
&& cal.get(Calendar.SECOND) == Integer.valueOf(
jta7.getText().toString()).intValue())) {
String strCommand = jta8.getText().toString();
try {
task2 = false;
Thread.sleep(1000);
Runtime.getRuntime().exec(strCommand);
} catch (Exception ex) {
}
}
}
System.exit(0);
}
}
public Timing() {
// TODO Auto-generated constructor stub
jp.setLayout(null);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.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, 130, 80, 20);
jl5.setBounds(10, 70, 60, 20);
jta5.setBounds(80, 70, 40, 20);
jl6.setBounds(130, 70, 20, 20);
jta6.setBounds(160, 70, 40, 20);
jl7.setBounds(210, 70, 20, 20);
jta7.setBounds(240, 70, 40, 20);
jl8.setBounds(290, 70, 20, 20);
jta8.setBounds(10, 100, 300, 20);
jb3.setBounds(320, 100, 80, 20);
fileChooser.removeChoosableFileFilter(fileChooser
.getChoosableFileFilters()[0]);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(".exe",
"exe"));
this.add(jp);
this.setTitle("定时启动器");
this.setBounds(110, 110, 500, 200);
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();
jta4.setText(path);
}
if (e.getSource() == jb3) {
fileChooser.showOpenDialog(this);
String path = fileChooser.getSelectedFile().getPath().toString();
jta8.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{if (jta5.getText().equals("") && jta6.getText().equals("")
&& jta7.getText().equals("") && jta8.getText().equals("")) {
task2 = false;
}
alarm al = new alarm();
al.start();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Timing();
}
}