2012-07-07 20 views

回答

4

1.创建一个与GUI线程分开的线程(即事件调度程序线程)。

2.让它调用给予雅虎股票以秒延迟60服务,使用Thread.sleep(60000);

呼叫repaint();

编辑:

new Thread(new Runnable(){ 

    public void run(){ 

    try{ 
      while (true){ 

     Thread.sleep(60000); 
     yahoo() // CALL TO YAHOO SENSEX 

     repaint(); 
      } 
     }catch(Exception ex){ 

     } 
}).start(); 
+0

你能举一个小例子吗?Thnx。 – skiabox 2012-07-07 19:23:10

+0

那么我在哪里放置这段代码?在主JFrame窗口内? – skiabox 2012-07-07 19:38:59

+0

了java.awt.EventQueue.invokeLater(新的Runnable(){ 公共无效的run(){ 新的MainForm()调用setVisible(真);} }); – skiabox 2012-07-07 19:39:23

2

使用TimerTimerTask类定期做一些行动。并发送“更新”事件到你的应用程序的视图组件(以MVC标记)。

1

可能都在使用的最佳解决方案一个新的线程,即下载信息&重绘本身..

2

你可以开始为暴露在样品上的事件堆栈上的新Thread并在此线程执行,你可以把事件下面。实现这种方式更安全,因为GUI更新将从事件派发线程中调用。更多信息here

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class SpringcConc { 

    final static JLabel label = new JLabel("INITIALIZED ..."); 

    public static void main(final String[] s) { 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame myJframe = new JFrame(); 
       myJframe.add(label); 
       myJframe.pack(); 
       myJframe.setVisible(true); 
       instituteThreads(); 
      } 
     }); 

    } 

    protected static void instituteThreads() { 
     Thread queryThread1 = new Thread() { 
      @Override 
      public void run() { 
       while (true) { 
        try { 
         sleep(500); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        SwingUtilities.invokeLater(new Runnable() { 
         public void run() { 
          label.setText("THREAD 1 UPD"); 
         } 
        }); 
       } 
      } 
     }; 
     queryThread1.start(); 
     Thread queryThread2 = new Thread() { 
      @Override 
      public void run() { 
       while (true) { 
        try { 
         sleep(750); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        SwingUtilities.invokeLater(new Runnable() { 
         public void run() { 
          label.setText("THREAD 2 UPD"); 
         } 
        }); 
       } 
      } 
     }; 
     queryThread2.start(); 
    } 

} 

另一种方法是使用Timer

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class SwingTimer { 

    public static void main(final String[] args) { 
     final JLabel label = new JLabel("INITIALIZED"); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame jFrame = new JFrame(); 
       jFrame.add(label); 
       jFrame.pack(); 
       jFrame.setVisible(true); 
       jFrame.setBounds(200, 200, 100, 50); 
      } 
     }); 
     new Timer(500, new ActionListener() { 
      public void actionPerformed(final ActionEvent e) { 
       label.setText("UPDATE 1"); 
      } 
     }).start(); 
     new Timer(750, new ActionListener() { 
      public void actionPerformed(final ActionEvent e) { 
       label.setText("UPDATE 2"); 
      } 
     }).start(); 
    } 

} 
+0

谢谢,我会试试看。 – skiabox 2012-07-07 19:43:48

+0

我刚刚更新了示例,可以放入'run'方法。 – 2012-07-07 19:46:50

2

而不是一个单独的线程或java.util.Timer,使用javax.swing.Timer踱步更新您的数据模型,如图所示here。优点是“Swing定时器的任务在事件分派线程中执行”。

image

相关问题