2013-06-22 36 views
1

我目前有一組TickerSymbol()對象,它們在Swing JFrame中的屏幕上滾動。我不知道如何讓它在邏輯上得到包裝。在Swing中製作股票代碼環繞

protected void animate() { 
    new Timer(TIMER_DELAY, new TimerListener()).start(); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    g.setFont(BG_STRING_FONT); 
    g.setColor(Color.LIGHT_GRAY); 
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
      RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
    List<TickerSymbol> tickers = ticker.getTickers(); 
    synchronized(tickers){ 
     for(TickerSymbol ts : tickers) { 
      //Create formatted string with ticker info 
      // This part works fine 
      ... 
     } 
    } 

} 

private class TimerListener implements ActionListener { 
    public void actionPerformed(java.awt.event.ActionEvent e) { 
     //TODO need to make the ticker wrap around rather than hard coding. 
     if (imgX <= -2000) { 
      imgX = getWidth(); 
     } else { 
      imgX -= 1; 
     } 

     repaint(); 
    }; 
} 

正如你在上面看到的,我目前已經對'imgX'重置爲getWidth()的位置進行了硬編碼。這意味着字符串會將其位置重置到應用程序的右側。

我試過從Guava項目中使用「Iterables.cycle」,但它最終導致內存溢出。

Iterable<TickerSymbol> live = Iterables.cycle(tickers); 
     while(live.iterator().hasNext()) { 
     .... 

任何幫助表示讚賞!

+1

['MarqueeTest'](http://stackoverflow.com/a/3621417/230513)就是一個例子。 – trashgod

+0

這正是我想要的!如果你願意發佈這個答案,我會很樂意接受它 –

回答

1

查看Marquee Panel。它允許您滾動包含組件的面板。因此,您可以使用包含要滾動的文本的一個JLabel創建面板,也可以向面板添加多個標籤,以便在滾動字幕時動態更新標籤。

+0

完美!我主要在尋找環繞功能,這似乎很好地實現了它。 –