2011-12-01 61 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>"));