linklabel.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 101 行

SVN-BASE
101
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.spark.component;

import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 * The <code>LinkLabel</code> class is a JLabel subclass
 * to mimic an html link. When clicked, it launches the specified url
 * in the default browser.
 *
 * @author Derek DeMoro
 */
final public class LinkLabel extends JLabel implements MouseListener {
    
    // cursors used in url-link related displays and default display
    private Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
    private Cursor LINK_CURSOR = new Cursor(Cursor.HAND_CURSOR);
    private Color rolloverTextColor;
    private Color foregroundTextColor;
    private String labelURL;

    private boolean invokeBrowser;

    /**
     * @param text            the text for the label.
     * @param url             the url to launch when this label is clicked on.
     * @param foregroundColor the text color for the label.
     * @param rolloverColor   the text color to be used when the mouse is over the label.
     */
    public LinkLabel(String text,
                     String url,
                     Color foregroundColor,
                     Color rolloverColor) {
        super(text);

        rolloverTextColor = rolloverColor;
        foregroundTextColor = foregroundColor;
        labelURL = url;

        this.addMouseListener(this);

        this.setForeground(foregroundTextColor);
        this.setVerticalTextPosition(JLabel.TOP);
    }

    public void setInvokeBrowser(boolean invoke) {
        invokeBrowser = invoke;
    }

    /**
     * MouseListener implementation.
     *
     * @param me the MouseEvent.
     */
    public void mouseClicked(MouseEvent me) {
        if (invokeBrowser) {
            try {
                BrowserLauncher.openURL(labelURL);
            }
            catch (IOException e) {
                Log.error(e);
            }
        }
    }

    public void mousePressed(MouseEvent me) {
    }

    public void mouseReleased(MouseEvent me) {
    }

    public void mouseEntered(MouseEvent me) {
        this.setForeground(rolloverTextColor);
        this.setCursor(LINK_CURSOR);
    }

    public void mouseExited(MouseEvent me) {
        this.setForeground(foregroundTextColor);
        this.setCursor(DEFAULT_CURSOR);
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?