java - MarqueePanel 中的 JEditorPane/JTextPane

标签 java swing jtextpane marquee

我正在尝试在 JFrame 中滚动文本,并且文本本身使用 MarqueePanel 包含超链接。类(class)。然而,虽然滚动工作正常,但链接似乎都在固定位置。

我尝试使用 JEditorPane 和 JTextPane,虽然外观正确,但超链接似乎根本没有移动。这是可以完成的事情吗?

编辑:代码如下 - MarqueePanel 类链接位于上方。

public static void Main(String[] args)
{

    JFrame w = new JFrame("跑马灯测试");
    尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    w.setSize(screenSize.width, (int) ((float) .04 * (float) screenSize.height));

    MarqueePanel mp = new MarqueePanel(22,2);

    JEdi​​torPane jep = new JEditorPane("text/html", "测试链接编号 1  测试链接号 2");
    jep.setOpaque(假);
    jep.setEditable(假);
    jep.addMouseListener(mp);

    jep.addHyperlinkListener(new HyperlinkListener() {
            公共(public)无效超链接更新(HyperlinkEvent e){
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    if (Desktop.isDesktopSupported()) {
                        尝试 {
                            Desktop.getDesktop().browse(e.getURL().toURI());
                        } catch (IOException e1) {
                           //TODO 自动生成的 catch  block 
                            e1.printStackTrace();
                        } catch (URISyntaxException e1) {
                           //TODO 自动生成的 catch  block 
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });
    mp.add(jep);
    w.add(mp);
    w.pack();
    w.setVisible(true);
}

最佳答案

Marquee Panel 并不是为处理事件而设计的,因为它只是在不同的位置呈现每个组件以提供滚动效果。

这是一个尝试转换 MouseEvent 并将事件重新分派(dispatch)回原始组件的版本:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *  The MarqueePanelMouse is used to scroll components from the right edge of the
 *  panel to the left edge. Scrolling is continuous. To simulate the scrolling
 *  of text you can simply add a JLabel to the panel.
 *
 *  Various properties control the scrolling of the components on the panel.
 *  Changes to the properties are dynamic and will take effect the next time
 *  the components are scrolled.
 */
public class MarqueePanelMouse extends JPanel
    implements ActionListener, AncestorListener, WindowListener, MouseListener, MouseMotionListener
{
    protected boolean paintChildren;
    protected boolean scrollingPaused;
    protected int scrollOffset;
    protected int wrapOffset;

    private int preferredWidth = -1;
    private int scrollAmount;
    private int scrollFrequency;
    private boolean wrap = false;
    private int wrapAmount = 50;
    private boolean scrollWhenFocused = true;
    private Timer timer = new Timer(1000, this);

    /**
     *  Convenience constructor that sets both the scroll frequency and
     *  scroll amount to a value of 5.
     */
    public MarqueePanelMouse()
    {
        this(5, 5);
    }

    /**
     *  Create an AnimatedIcon that will continuously cycle with the
     * default (500ms).
     *
     *  @param component  the component the icon will be painted on
     *  @param icons      the Icons to be painted as part of the animation
     */
    public MarqueePanelMouse(int scrollFrequency, int scrollAmount)
    {
        setScrollFrequency( scrollFrequency );
        setScrollAmount( scrollAmount );
        setLayout( new BoxLayout(this, BoxLayout.X_AXIS) );
        addAncestorListener( this );
//      addMouseListener( this );
//      addMouseMotionListener( this );
    }

    /*
     *  Translate the location of the children before they are painted so it
     *  appears they are scrolling left to right
     */
    @Override
    public void paintChildren(Graphics g)
    {
        //  Need this so we don't see a flicker of the text before scrolling

        if (! paintChildren) return;

        //  Normal painting as the components scroll right to left

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(-scrollOffset, 0);
        super.paintChildren(g);
        g2d.translate(scrollOffset, 0);

        //  Repaint the start of the components on the right edge of the panel once
        //  all the components are completely visible on the panel.
        //  (Its like the components are in two places at the same time)

        if (isWrap())
        {
            wrapOffset = scrollOffset - super.getPreferredSize().width - wrapAmount;
            g2d.translate(-wrapOffset, 0);
            super.paintChildren(g);
            g2d.translate(wrapOffset, 0);
        }
    }

    /*
     *  The default preferred size will be half the size of the components added to
     *  the panel. This will allow room for components to be scrolled on and off
     *  the panel.
     *
     *  The default width can be overriden by using the setPreferredWidth() method.
     */
    @Override
    public Dimension getPreferredSize()
    {
        Dimension d = super.getPreferredSize();

        d.width = (preferredWidth == -1) ? d.width / 2 : preferredWidth;

        return d;
    }

    @Override
    public Dimension getMinimumSize()
    {
        return getPreferredSize();
    }

    public int getPreferredWidth()
    {
        return preferredWidth;
    }

    /**
     *  Specify the preferred width on the panel. A value of -1 will cause the
     *  default preferred with size calculation to be used.
     *
     *  @param preferredWidth  preferred width of the panel in pixels
     */
    public void setPreferredWidth(int preferredWidth)
    {
        this.preferredWidth = preferredWidth;
        revalidate();
    }

    /**
     *  Get the scroll amount.
     *
     *  @return the scroll amount in pixels
     */
    public int getScrollAmount()
    {
        return scrollAmount;
    }

    /**
     *  Specify the scroll amount. The number of pixels to scroll every time
     *  scrolling is done.
     *
     *  @param scrollAmount  scroll amount in pixels
     */
    public void setScrollAmount(int scrollAmount)
    {
        this.scrollAmount = scrollAmount;
    }

    /**
     *  Get the scroll frequency.
     *
     *  @return the scroll frequency
     */
    public int getScrollFrequency()
    {
        return scrollFrequency;
    }

    /**
     *  Specify the scroll frequency. That is the number of times scrolling
     *  should be performed every second.
     *
     *  @param scrollFrequency  scroll frequency
     */
    public void setScrollFrequency(int scrollFrequency)
    {
        this.scrollFrequency = scrollFrequency;

        int delay = 1000 / scrollFrequency;
        timer.setInitialDelay( delay );
        timer.setDelay( delay );
    }

    /**
     *  Get the scroll only when visible property.
     *
     *  @return the scroll only when visible value
     */
    public boolean isScrollWhenFocused()
    {
        return scrollWhenFocused;
    }

    /**
     *  Specify the scrolling property for unfocused windows.
     *
     *  @param scrollWhenVisible  when true scrolling pauses when the window
     *                              loses focus. Scrolling will continue when
     *                              the window regains focus. When false
     *                              scrolling is continuous unless the window
     *                              is iconified.
     */
    public void setScrollWhenFocused(boolean scrollWhenFocused)
    {
        this.scrollWhenFocused = scrollWhenFocused;
    }

    /**
     *  Get the wrap property.
     *
     *  @return the wrap value
     */
    public boolean isWrap()
    {
        return wrap;
    }

    /**
     *  Specify the wrapping property. Normal scrolling is such that all the text
     *  will scroll from left to right. When the last part of the text scrolls off
     *  the left edge scrolling will start again from the right edge. Therefore
     *  there is a time when the component is blank as nothing is displayed.
     *  Wrapping implies that as the end of the text scrolls off the left edge
     *  the beginning of the text will scroll in from the right edge. So the end
     *  and the start of the text is displayed at the same time.
     *
     *  @param wrap  when true the start of the text will scroll in from the right
     *              edge while the end of the text is still scrolling off the left
     *              edge. Otherwise the panel must be clear of text before
     *              will begin again from the right edge.
     */
    public void setWrap(boolean wrap)
    {
        this.wrap = wrap;
    }

    /**
     *  Get the wrap amount.
     *
     *  @return the wrap amount value
     */
    public int getWrapAmount()
    {
        return wrapAmount;
    }

    /**
     *  Specify the wrapping amount. This specifies the space between the end of the
     *  text on the left edge and the start of the text from the right edge when
     *  wrapping is turned on.
     *
     *  @param wrapAmount  the amount in pixels
     */
    public void setWrapAmount(int wrapAmount)
    {
        this.wrapAmount = wrapAmount;
    }

    /**
     *  Start scrolling the components on the panel. Components will start
     *  scrolling from the right edge towards the left edge.
     */
    public void startScrolling()
    {
        paintChildren = true;
        scrollOffset =  - getSize().width;

        timer.start();
    }

    /**
     *  Stop scrolling the components on the panel. The conponents will be
     *  cleared from the view of the panel
     */
    public void stopScrolling()
    {
        timer.stop();
        paintChildren = false;
        repaint();
    }

    /**
     *  The components will stop scrolling but will remain visible
     */
    public void pauseScrolling()
    {
        if (timer.isRunning())
        {
            timer.stop();
            scrollingPaused = true;
        }
    }

    /**
     *  The components will resume scrolling from where scrolling was stopped.
     */
    public void resumeScrolling()
    {
        if (scrollingPaused)
        {
            timer.restart();
            scrollingPaused = false;
        }
    }

    @Override
    public Component getComponentAt(int x, int y)
    {
        Point translated = getTranslatedPoint(x, y);

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

        return null;
    }

    public Point getTranslatedPoint(int x, int y)
    {
        int translatedX = x + scrollOffset;

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

        return new Point(translatedX, y);
    }

//  Implement ActionListener

    /**
     *  Adjust the offset of the components on the panel so it appears that
     *  they are scrolling from right to left.
     */
    public void actionPerformed(ActionEvent ae)
    {
        scrollOffset = scrollOffset + scrollAmount;
        int width = super.getPreferredSize().width;

        if (scrollOffset > width)
        {
            scrollOffset = isWrap() ? wrapOffset + scrollAmount : - getSize().width;
        }

        repaint();
    }

//  Implement AncestorListener

    /**
     *  Get notified when the panel is added to a Window so we can use a
     *  WindowListener to automatically start the scrolling of the components.
     */
    public void ancestorAdded(AncestorEvent e)
    {
        SwingUtilities.windowForComponent( this ).addWindowListener( this );
    }

    public void ancestorMoved(AncestorEvent e) {}
    public void ancestorRemoved(AncestorEvent e) {}

//  Implement WindowListener

    public void windowActivated(WindowEvent e)
    {
        if (isScrollWhenFocused())
            resumeScrolling();
    }

    public void windowClosed(WindowEvent e)
    {
        stopScrolling();
    }

    public void windowClosing(WindowEvent e)
    {
        stopScrolling();
    }

    public void windowDeactivated(WindowEvent e)
    {
        if (isScrollWhenFocused())
            pauseScrolling();
    }

    public void windowDeiconified(WindowEvent e)
    {
        resumeScrolling();
    }

    public void windowIconified(WindowEvent e)
    {
        pauseScrolling();
    }

    public void windowOpened(WindowEvent e)
    {
        startScrolling();
    }

//  Implement MouseMotionListener

    public void mouseMoved(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    public void mouseDragged(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    public void mouseClicked(MouseEvent e)
    {
        redispatchMouseEvent(e);
        System.out.println("clicked");
    }

    public void mouseEntered(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    public void mouseExited(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    public void mousePressed(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        redispatchMouseEvent(e);
    }

    private void redispatchMouseEvent(MouseEvent e)
    {
        int eventID = e.getID();

        Component component = getComponentAt( e.getX(), e.getY() );

        if (component == null) return;

        Point translatedPoint = getTranslatedPoint( e.getX(), e.getY() );
        Point componentPoint = SwingUtilities.convertPoint(this, translatedPoint, component);
        System.out.println(eventID + " : " + componentPoint);

        MouseEvent me = new MouseEvent(
            component,
            e.getID(),
            e.getWhen(),
            e.getModifiers(),
            componentPoint.x,
            componentPoint.y,
            e.getClickCount(),
            e.isPopupTrigger()
        );

        component.dispatchEvent( me );
    }
/*
    @Override
    public boolean isOptimizedDrawingEnabled()
    {
        return false;
    }
*/
}

此版本尚未经过调试或全面测试,因此它可能适合您,也可能不适合您。

关于java - MarqueePanel 中的 JEditorPane/JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782443/

相关文章:

java - Spring Data JPA - 不带参数的查询

java - 如何去除底部多余的空间

java - 无法在 JPanel 上设置 JLabel 的位置

java - 需要帮助将数组 imageicon 解析为 JButton

java - 如何用逗号替换中间的句号?

Java 独立网络摄像头应用程序

Java JOGL 纹理对象未翻译

java - 如何将 Java Swing 中的键盘输入映射到另一个字符或字符串?

java - JTextPane 中的突出显示和粗体文本

java - JTextPane/JEditorPane 和奇怪的文本问题