Auto loop-scrolling active contents in JPanel - marquee text

蹲街弑〆低调 提交于 2019-12-24 03:26:32

问题


I need some auto loop-scrolling (marquee text) it's contents JPanel. It contents must react on mouse clicking on different elements. So just drawing contents with moved coordinates not working here, because real position of elements not changing. Also it must be update able. Most likely that it will be smooth update - without any bouncing. Tried to use JScrollPane with no visible scrollers and auto-scrolling, it can hold action listeners, but I can't make it smooth looping and smooth updating contents.

UPDATE it should looks like this:

http://h1.flashvortex.com/display.php?id=2_1311593920_25605_144_0_700_30_6_1_92

but with modifying contents from code, without stopping animation and bouncing.


回答1:


You might be able to use the Marquee Panel. The code uses real components so you should be able to add and react to any listener you add to the components.

Edit:

Oops, I don't know what I was thinking, my code uses the Graphics.translate(...) method to paint the components so using a MouseListener directly won't work.

Edit2:

Maybe the following code will help. Just add the method to the MarqueePanel class:

public Component getComponentAtPoint(Point p)
{
    int translatedX = p.x + scrollOffset;

    if (isWrap())
    {
        int preferredWidth = super.getPreferredSize().width;
        preferredWidth += getWrapAmount();
        translatedX = translatedX % preferredWidth;
    }

    Point translated = new Point(translatedX, p.y);

    for (Component c: getComponents())
    {
        if (c.getBounds().contains(translated))
            return c;
    }

    return null;
}

Now you can add a MouseListener to the MarqueePanel and then invoke this method in order to determine which component the MouseEvent was generated for. Once you know which component was clicked you will manually need to invoke an Action for that component. Or you could try redispatching the MouseEvent to the component. You wouuld need to recreate the MouseEvent to make the component the source of the event instead of the panel being the source. You would also need to covert the event X/Y locations to be relative to the component and not the panel. The SwingUtils class should help with this.




回答2:


MarqueePanel includes start() and stop() methods; it might make a useful starting point, but you'll have to factor out an update() method.

Addendum: As the example uses a JLabel, it cannot be edited in situ. If using a JTextField, it may be easiest to update the corresponding model, Document.



来源:https://stackoverflow.com/questions/6814417/auto-loop-scrolling-active-contents-in-jpanel-marquee-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!