JOptionPane中的可点击链接

pst*_*ton 24 java swing

我正在使用JOptionPane来显示一些产品信息,并需要添加一些链接到网页.

我已经发现你可以使用包含html的JLabel,所以我已经包含了一个<a href>链接.该对话框中的链接显示为蓝色和带下划线,但不可点击.

例如,这也应该有效:

public static void main(String[] args) throws Throwable
{
    JOptionPane.showMessageDialog(null, "<html><a href=\"http://google.com/\">a link</a></html>");
}
Run Code Online (Sandbox Code Playgroud)

如何在JOptionPane中获得可点击的链接?

谢谢,保罗.

编辑 - 例如解决方案

public static void main(String[] args) throws Throwable
{
    // for copying style
    JLabel label = new JLabel();
    Font font = label.getFont();

    // create some css from the label's font
    StringBuffer style = new StringBuffer("font-family:" + font.getFamily() + ";");
    style.append("font-weight:" + (font.isBold() ? "bold" : "normal") + ";");
    style.append("font-size:" + font.getSize() + "pt;");

    // html content
    JEditorPane ep = new JEditorPane("text/html", "<html><body style=\"" + style + "\">" //
            + "some text, and <a href=\"http://google.com/\">a link</a>" //
            + "</body></html>");

    // handle link events
    ep.addHyperlinkListener(new HyperlinkListener()
    {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
                ProcessHandler.launchUrl(e.getURL().toString()); // roll your own link launcher or use Desktop if J6+
        }
    });
    ep.setEditable(false);
    ep.setBackground(label.getBackground());

    // show
    JOptionPane.showMessageDialog(null, ep);
}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 16

您可以将任何组件添加到JOptionPane.

因此,添加一个JEditorPane,它显示您的HTML并支持HyperlinkListener.


Jea*_*ana 6

答案下方提出的解决方案不适用于 Nimbus 外观和感觉。Nimbus 覆盖背景颜色并使背景变为白色。解决办法是在css中设置背景色。您还需要删除组件边框。这是一个实现与 Nimbus 一起使用的解决方案的类(我没有检查其他 L&F):

import java.awt.Color;
import java.awt.Font;

import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class MessageWithLink extends JEditorPane {
    private static final long serialVersionUID = 1L;

    public MessageWithLink(String htmlBody) {
        super("text/html", "<html><body style=\"" + getStyle() + "\">" + htmlBody + "</body></html>");
        addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    // Process the click event on the link (for example with java.awt.Desktop.getDesktop().browse())
                    System.out.println(e.getURL()+" was clicked");
                }
            }
        });
        setEditable(false);
        setBorder(null);
    }

    static StringBuffer getStyle() {
        // for copying style
        JLabel label = new JLabel();
        Font font = label.getFont();
        Color color = label.getBackground();

        // create some css from the label's font
        StringBuffer style = new StringBuffer("font-family:" + font.getFamily() + ";");
        style.append("font-weight:" + (font.isBold() ? "bold" : "normal") + ";");
        style.append("font-size:" + font.getSize() + "pt;");
        style.append("background-color: rgb("+color.getRed()+","+color.getGreen()+","+color.getBlue()+");");
        return style;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));
Run Code Online (Sandbox Code Playgroud)


小智 5

在这里看起来相当彻底地讨论了如何在JLabel中添加超链接