| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 732 人关注过本帖
标题:定时启动软件问题
只看楼主 加入收藏
hf8813264
Rank: 1
等 级:新手上路
帖 子:22
专家分:5
注 册:2009-6-2
结帖率:33.33%
收藏
 问题点数:0 回复次数:3 
定时启动软件问题
想通过java编写一个用户界面,通过设置具体的时间,来运行文件选择器里选定的文件,可是始终无法实现某些功能,现在有几个疑问:如何动态的获取系统的时间,以及如何真正的实现系统时间如自己设定时间的比较,还有就是如何在设定的时间运行选择的exe文件,基本代码在下面,麻烦大家看看
package ch10;

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 {
   

    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();
    }

}
搜索更多相关主题的帖子: java 软件 时间 用户 
2011-01-10 16:30
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
不懂

   唯实惟新 至诚致志
2011-01-10 21:34
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:0 
程序进入死循环了, 当前时间只能创建一次实例, 所以cal.HOUR和cal.MINUTE是没有变化的. while循环也就不能break
时间控制,改用线程是否好些?

[ 本帖最后由 lampeter123 于 2011-1-11 09:38 编辑 ]

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2011-01-11 08:50
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:0 
帮你改用Timer类 和 TimerTask类
程序代码:
package ch10;

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;
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();
        }
    }
}





你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2011-01-11 13:26
快速回复:定时启动软件问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021457 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved