如何向JLabel添加选取框行为

时间:2010-02-18 19:57:47

标签: java swing

如何在JLabel的文字中添加选框行为?

我试过这个

JLabel search = new JLabel("<html><marquee>Search</marquee><html>");

但它不起作用。

6 个答案:

答案 0 :(得分:3)

有关如何执行此操作的详细信息,请参阅http://forums.sun.com/thread.jspa?forumID=57&threadID=605616。)

(编辑:我可能会直接在paint()方法中使用System.currentTimeMillis()而不是使用计时器,然后将/ modulo(%)除以使其进入'x offset'所需的范围例子)。通过增加分区数的大小,您可以更改速度((System.currentTimeMillis()/ 200)%50)。

(编辑2:我刚刚更新了下面的代码以解决重新绘制的问题。现在我们在paint方法中安排重绘;也许有更好的方法,但这可行:))

(编辑3:错误,我尝试了更长的字符串,它搞砸了。这很容易修复(再次按宽度增加范围以补偿负值,减去宽度)

package mt;

import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;

public class MyJLabel extends JLabel {
    public static final int MARQUEE_SPEED_DIV = 5;
    public static final int REPAINT_WITHIN_MS = 5;

    /**
     * 
     */
    private static final long serialVersionUID = -7737312573505856484L;

    /**
     * 
     */
    public MyJLabel() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @param image
     * @param horizontalAlignment
     */
    public MyJLabel(Icon image, int horizontalAlignment) {
        super(image, horizontalAlignment);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param image
     */
    public MyJLabel(Icon image) {
        super(image);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param text
     * @param icon
     * @param horizontalAlignment
     */
    public MyJLabel(String text, Icon icon, int horizontalAlignment) {
        super(text, icon, horizontalAlignment);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param text
     * @param horizontalAlignment
     */
    public MyJLabel(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param text
     */
    public MyJLabel(String text) {
        super(text);
    }



    /* (non-Javadoc)
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    @Override
    protected void paintComponent(Graphics g) {
        g.translate((int)((System.currentTimeMillis() / MARQUEE_SPEED_DIV) % (getWidth() * 2)) - getWidth(), 0);
        super.paintComponent(g);
        repaint(REPAINT_WITHIN_MS);
    }
}

答案 1 :(得分:2)

这很好用(我自己写的,所以可能会有一些错误):

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class JMarqueeLabel extends JPanel implements Runnable
{
    /**
     * 
     */
    private static final long serialVersionUID = -2973353417536204185L;
    private int x;
    private FontMetrics fontMetrics;
    public static final int MAX_SPEED = 30;
    public static final int MIN_SPEED = 1;
    private int speed;
    public static final int SCROLL_TO_LEFT = 0;
    public static final int SCROLL_TO_RIGHT = 1;
    private int scrollDirection = 0;
    private boolean started = false;
    private JLabel label;

    public JMarqueeLabel(String text)
    {
        super();
        label = new JLabel(text)
        {
            /**
             * 
             */
            private static final long serialVersionUID = -870580607070467359L;

            @Override
            protected void paintComponent(Graphics g)
            {
                g.translate(x, 0);
                super.paintComponent(g);
            }
        };
        setLayout(null);
        add(label);
        setSpeed(10);
        setScrollDirection(SCROLL_TO_RIGHT);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        label.paintComponents(g);
    }

    public void setScrollDirection(int scrollDirection)
    {
        this.scrollDirection = scrollDirection;
    }

    public int getScrollDirection()
    {
        return scrollDirection;
    }

    public void setSpeed(int speed)
    {
        if (speed < MIN_SPEED || speed > MAX_SPEED)
        {
            throw new IllegalArgumentException("speed out of range");
        }
        this.speed = speed;
    }

    public int getSpeed()
    {
        return speed;
    }

    @Override
    public void validateTree()
    {
        System.out.println("Validate...");
        super.validateTree();
        label.setBounds(0, 0, 2000, getHeight());
        if (!started)
        {
            x = getWidth() + 10;
            Thread t = new Thread(this);
            t.setDaemon(true);
            t.start();
            started = true;
        }
    }

    public String getText()
    {
        return label.getText();
    }

    public void setText(String text)
    {
        label.setText(text);
    }

    public void setTextFont(Font font)
    {
        label.setFont(font);
        fontMetrics = label.getFontMetrics(label.getFont());
    }

    @Override
    public void run()
    {
        fontMetrics = label.getFontMetrics(label.getFont());
        try
        {
            Thread.sleep(100);
        } catch (Exception e)
        {
        }
        while (true)
        {
            if (scrollDirection == SCROLL_TO_LEFT)
            {
                x--;
                if (x < -fontMetrics.stringWidth(label.getText()) - 10)
                {
                    x = getWidth() + 10;
                }
            }
            if (scrollDirection == SCROLL_TO_RIGHT)
            {
                x++;
                if (x > getWidth() + 10)
                {
                    x = -fontMetrics.stringWidth(label.getText()) - 10;
                }
            }
            repaint();
            try
            {
                Thread.sleep(35 - speed);
            } catch (Exception e)
            {
            }
        }
    }

}

这适用于非常长的字符串。如果您需要更长的字符串,则必须将2000更改为更高的数字。

希望这是你想要的: - )

答案 2 :(得分:1)

如您所见,JLabel上的HTML仅限于格式化,不支持<marquee>标记。您必须使用类似SwingWorkerExecutorService的内容每隔几毫秒更改一次文本。

答案 3 :(得分:1)

我在网上的found a solution至少对我有用: - )

答案 4 :(得分:1)

Java中的Marquee标签,用于控制流量,速度和文本的方向 这篇文章How to create marquee JLabel using java

答案 5 :(得分:0)

hier是一个快速的肮脏解决方案。 如果需要,我可以在某些方面精确地编写代码。


package gui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class ScrollLabel extends JPanel {
    private static final long   serialVersionUID    = 3986350733160423373L;
    private final JLabel        label;
    private final JScrollPane   sPane;
    private final ScrollMe      scrollMe;

    public ScrollLabel(String text) {
        setLayout(new GridLayout(1, 1));
        label = new JLabel(text);
        sPane = new JScrollPane(label);
        sPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        JScrollBar horizontalScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
        sPane.setHorizontalScrollBar(horizontalScrollBar);
        setBorder(BorderFactory.createLineBorder(Color.RED));
        add(sPane.getViewport());
        scrollMe = new ScrollMe(this, label, sPane);
        scrollMe.start();
    }

    class ScrollMe extends Thread {
        JScrollPane sPane;
        ScrollLabel parent;
        JLabel      label;

        ScrollMe(ScrollLabel parent, JLabel label, JScrollPane sPane) {
            this.sPane = sPane;
            this.parent = parent;
            this.label = label;
        }

        boolean isRunning   = true;

        @Override
        public void run() {
            int pos = 0;
            try {
                while (isRunning) {
                    if (sPane.isVisible()) {
                        int sWidth = parent.getSize().width;
                        int lWidth = label.getWidth();

                        if (sWidth < lWidth) {
                            if (pos + sWidth > lWidth)
                                pos = 0;
                            sPane.getHorizontalScrollBar().setValue(pos);
                        }
                    }
                    synchronized (this) {
                        wait(50);
                        pos += 1;
                    }
                }
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                isRunning = false;
            }
        }
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("ScrollLabel");
        jFrame.getContentPane().add(new ScrollLabel("how to add marquee behaviour to Jlabel !Java"));
        jFrame.setVisible(true);
    }
}