在Swing中制作股票报价器

时间:2013-06-22 18:37:00

标签: java swing

我目前有一组TickerSymbol()对象在我的Swing JFrame中滚动屏幕。我不知道如何在逻辑方面将它包裹起来。

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()的位置。这意味着String将其位置重置到应用程序的右上角。

我尝试过使用Guava项目中的“Iterables.cycle”,但最终导致内存溢出。

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

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

查看Marquee Panel。它允许您滚动包含组件的面板。因此,您可以使用一个包含要滚动的文本的JLabel创建一个面板,或者可以向面板添加多个标签,这样您就可以在选取框滚动时动态更新标签。

相关问题