Java 作业窗格中的可单击链接

Java 作业窗格中的可单击链接,java,swing,Java,Swing,我正在使用JOptionPane显示一些产品信息,需要添加一些网页链接 我发现可以使用包含html的JLabel,所以我添加了一个“”; } 如何在JOptionPane中获取可点击链接 谢谢你,保罗 编辑-例如解决方案 public static void main(String[] args) throws Throwable { // for copying style JLabel label = new JLabel(); Font font = label.

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

我发现可以使用包含html的JLabel,所以我添加了一个“
”;
}
如何在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);
}
publicstaticvoidmain(String[]args)抛出Throwable
{
//用于复制样式
JLabel标签=新的JLabel();
Font=label.getFont();
//从标签的字体创建一些css
StringBuffer样式=新的StringBuffer(“字体系列:“+font.getFamily()+”;”);
style.append(“font-weight:+(font.isBold()?“bold:“normal”)+”;”;
style.append(“字体大小:”+font.getSize()+“pt;”);
//html内容
JEditorPane ep=新的JEditorPane(“文本/html”和“//
+“一些文本,以及”//
+ "");
//处理链接事件
ep.addHyperlinkListener(新的HyperlinkListener()
{
@凌驾
公共无效hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
ProcessHandler.launchUrl(例如getURL().toString());//滚动您自己的链接启动器或使用桌面(如果是J6)+
}
});
ep.setEditable(假);
ep.setBackground(label.getBackground());
//展示
showMessageDialog(null,ep);
}

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


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

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

导入java.awt.Color;
导入java.awt.Font;
导入javax.swing.JEditorPane;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.event.HyperlinkEvent;
导入javax.swing.event.HyperlinkListener;
公共类MessageWithLink扩展了TorPane{
私有静态最终长serialVersionUID=1L;
公共消息WithLink(字符串htmlBody){
超级(“文本/html”,“html正文+”);
addHyperlinkListener(新的HyperlinkListener(){
@凌驾
公共无效hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)){
//处理链接上的单击事件(例如,使用java.awt.Desktop.getDesktop().browse())
System.out.println(e.getURL()+“已单击”);
}
}
});
可编辑设置(假);
订单号(空);
}
静态StringBuffer getStyle(){
//用于复制样式
JLabel标签=新的JLabel();
Font=label.getFont();
Color=label.getBackground();
//从标签的字体创建一些css
StringBuffer样式=新的StringBuffer(“字体系列:“+font.getFamily()+”;”);
style.append(“font-weight:+(font.isBold()?“bold:“normal”)+”;”;
style.append(“字体大小:”+font.getSize()+“pt;”);
追加(“背景色:rgb(“+color.getRed()+”、“+color.getGreen()+”、“+color.getBlue()+”)”);
回归风格;
}
}
用法:

JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));
JOptionPane.showMessageDialog(null,newmessagewithlink(“此处有一个链接”);

是的,这是迄今为止我找到的最简单的解决方案。这并不难,我将发布我的解决方案,例如在我的问题中为其他人提供的解决方案。aw Maaan!我将其作为另一个答案的注释发布!在发布的解决方案中,我找不到ProcessHandler类。它来自哪里?您可以使用
Desktop.getDesktop()。浏览(e.getURL().toURI())
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;
    }
}
JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));