我的想法是线程控制时间,时间的生成可以用随机类Random

解决方案 »

  1.   

    试一下吧,看可不可以,你刷新文件的操作可以放在Refresh()函数中
    我运行成功了,在按钮上显示的是刷新时间import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    public class Frame1 extends JFrame
    {    public ReplyTimeThread rtTread;
        JButton jBtn = new JButton();    public Frame1() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception
        {
            this.setBounds(200,100,300,400);
            rtTread = new ReplyTimeThread(this);
            jBtn.setBounds(new Rectangle(113, 103, 117, 43));
            jBtn.setText("show Random");
            this.setLocale(java.util.Locale.getDefault());
            this.getContentPane().setLayout(null);
            rtTread.start();
            this.getContentPane().add(jBtn, null);    }
        public void Refresh(int intNum)
        {
            jBtn.setText(intNum/1000+" second");
        }    public static void main(String[] args)
        {
            Frame1 fm = new Frame1();
            fm.setVisible(true);
            fm.addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });    }}class ReplyTimeThread extends Thread
    {
        Frame1 me;
        public int randomNum;
        Random randomTest = new Random();
        public ReplyTimeThread(Frame1 me)
        {
            this.me = me;
        }    public void run()
        {
            while(true)
            {
                try
                {
                    randomNum = randomTest.nextInt();
                    randomNum = randomNum /100000;
                    me.Refresh(randomNum);
                    Thread.sleep( randomNum );
                } catch (Exception e) {}
            }
        }}