我正在使用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);
}
答案 0 :(得分:16)
您可以将任何组件添加到JOptionPane。
所以添加一个JEditorPane,它显示你的HTML并支持HyperlinkListener。
答案 1 :(得分:5)
答案 2 :(得分:4)
The solution proposed below the answer does not work with Nimbus Look and Feel. Nimbus overrides the background color and makes the background white. The solution is to set the background color in the css. You also need to remove the component border. Here is a class that implements a solution that works with Nimbus (I Did not check other 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;
}
}
Usage:
JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));