JEdi​​torPane 中的超链接未在 MAC 打开

Hyperlink in JEditorPane not opening on MAC

我有一个 JEditorPane,显示在 JOptionPane 中,URL 我想在关闭我的应用程序之前打开它。它适用于 Windows 和 Linux,但不适用于 Mac。

这是代码:

//LINK
String link = "http://www.google.com/";
String link_name = "Google";

//Editor_Pane
JEditorPane editor_pane = new JEditorPane();
editor_pane.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
editor_pane.setText( /*some text*/  + "<a href=\"" + link + "\">" + link_name + "</a>");
editor_pane.setEditable(false);

//ADD A LISTENER
editor_pane.addHyperlinkListener(new HyperlinkListener(){
    public void hyperlinkUpdate(HyperlinkEvent e){
        if(e.getEventType() == (HyperlinkEvent.EventType.ACTIVATED)){

            //OPEN THE LINK
            try{ Desktop.getDesktop().browse(e.getURL().toURI());
            }catch (IOException | URISyntaxException e1) {e1.printStackTrace();}

            //EXIT
            System.exit(0);
        }
    }
});

//SHOW THE PANE
JOptionPane.showOptionDialog(null, editor_pane, "text", JOptionPane.DEFAULT_OPTION, 
                             JOptionPane.PLAIN_MESSAGE, null, new Object[] {}, null);

link 似乎可以点击,但是当我点击时没有任何反应,即使我尝试删除 Desktop.browse 方法并只保留 exit 方法。

我做错了什么?谢谢!

尝试添加:

editor_pane.setEditable(false);

窗格需要是只读的,链接才能被点击。有关详细信息,请参阅 JEditorPane

The HTML EditorKit will generate hyperlink events if the JEditorPane is not editable (JEditorPane.setEditable(false); has been called).

编辑:

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class TestLink {

    public static void main(String[] args) {
        JLabel label = new JLabel("Whosebug");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        label.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {  
                if (Desktop.isDesktopSupported()) {
                    try {
                      Desktop.getDesktop().browse(new URI("http://whosebug.com"));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                  } else { 
                      //TODO 
                  }
            }  
        }); 
        JOptionPane.showMessageDialog(null, label);
    }
}