在JPanel中自动循环滚动活动内容-字幕文本 [英] Auto loop-scrolling active contents in JPanel - marquee text

查看:184
本文介绍了在JPanel中自动循环滚动活动内容-字幕文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些自动循环滚动(字幕文本),其内容为JPanel.它的内容必须在鼠标单击不同元素时作出反应.因此,仅绘制具有移动坐标的内容在这里不起作用,因为元素的实际位置没有改变. 而且它必须具有更新能力.最有可能的是它将平滑更新-没有任何反弹. 尝试使用没有可见滚动条和自动滚动条的JScrollPane,它可以容纳动作侦听器,但是我无法使其平滑循环和平滑更新内容.

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.

推荐答案

您也许可以使用字幕面板.该代码使用了真实的组件,因此您应该能够对添加到组件中的任何侦听器进行添加并做出反应.

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.

糟糕,我不知道我在想什么,我的代码使用Graphics.translate(...)方法绘制组件,因此直接使用MouseListener无效.

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:

也许以下代码会有所帮助.只需将方法添加到MarqueePanel类中即可:

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;
}

现在,您可以将一个MouseListener添加到MarqueePanel中,然后调用此方法,以确定生成MouseEvent的组件.一旦知道单击了哪个组件,您将需要手动为该组件调用一个动作.或者,您可以尝试将MouseEvent重新分发到组件.您将需要重新创建MouseEvent,以使组件成为事件的源,而不是使面板成为源.您还需要隐瞒事件X/Y位置相对于组件而不是面板. SwingUtils类应对此提供帮助.

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.

这篇关于在JPanel中自动循环滚动活动内容-字幕文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆