Auto loop-scrolling active contents in JPanel - ma

2019-07-18 12:33发布

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.

2条回答
Animai°情兽
2楼-- · 2019-07-18 13:00

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.

查看更多
戒情不戒烟
3楼-- · 2019-07-18 13:04

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.

查看更多
登录 后发表回答