2011-12-01 102 views
20

我正在使用JOptionPane来显示一些产品信息,并需要添加一些链接到网页。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>"); 
} 

我如何获得的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); 
} 
+4

的解决方案张贴我无法找到类ProcessHandler。它从何而来? – alexandre1985

回答

15

您可以添加任何组件的JOptionPane。

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

+1

是的,这是迄今为止我找到的最简单的解决方案。它不难,我会发布我的解决方案,例如在我的问题为他人。 – pstanton

+0

aw maaaan!我张贴作为对其他答案的评论! –

5

貌似这是讨论相当彻底这里How to add hyperlink in JLabel

+0

showmessagedialog需要一个对象,如果它是一个控件,只需将它放入对话框中,这样就可以传递jlabel而不是文本本身。或者你甚至可以使用只读jeditorpane,它有很多html内容和链接,而不是只有一个标签。 –

3

答案下面提出的解决方案不适用于Nimbus Look and Feel。 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; 
    } 
} 

用法:

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