gpt4 book ai didi

java - JEdi​​torPane 中的超链接无法在 MAC 上打开

转载 作者:行者123 更新时间:2023-12-01 18:03:32 24 4
gpt4 key购买 nike

我有一个 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);

该链接似乎可以单击,但单击时没有任何反应,即使我尝试删除 Desktop.browse 方法并仅使用 exit 方法也是如此。

我做错了什么?谢谢!

最佳答案

尝试添加:

editor_pane.setEditable(false);

该 Pane 必须是只读的,链接才能可单击。请参阅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("stackoverflow");
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://stackoverflow.com"));
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
//TODO
}
}
});
JOptionPane.showMessageDialog(null, label);
}
}

关于java - JEdi​​torPane 中的超链接无法在 MAC 上打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38757224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com