下面列出了javax.swing.JTextPane#addHyperlinkListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static JTextPane createInfoPanel(String notification) {
JTextPane balloon = new JTextPane();
balloon.setContentType("text/html");
String text = getDetails(notification).replaceAll("(\r\n|\n|\r)", "<br>");
balloon.setText(text);
balloon.setOpaque(false);
balloon.setEditable(false);
balloon.setBorder(new EmptyBorder(0, 0, 0, 0));
if (UIManager.getLookAndFeel().getID().equals("Nimbus")) {
//#134837
//http://forums.java.net/jive/thread.jspa?messageID=283882
balloon.setBackground(new Color(0, 0, 0, 0));
}
balloon.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
URLDisplayer.getDefault().showURL(e.getURL());
}
}
});
return balloon;
}
public void setupPane(JTextPane pane, final File[] files, String fileNames, final File projectDir, final String url, final String revision) {
String msg = revision == null
? NbBundle.getMessage(NotificationsManager.class, "MSG_NotificationBubble_DeleteDescription", fileNames, CMD_DIFF) //NOI18N
: NbBundle.getMessage(NotificationsManager.class, "MSG_NotificationBubble_Description", fileNames, url, CMD_DIFF); //NOI18N
pane.setText(msg);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
if(CMD_DIFF.equals(e.getDescription())) {
Context ctx = new Context(files);
DiffAction.diff(ctx, Setup.DIFFTYPE_REMOTE, NbBundle.getMessage(NotificationsManager.class, "LBL_Remote_Changes", projectDir.getName()), false); //NOI18N
} else if (revision != null) {
try {
SearchHistoryAction.openSearch(new SVNUrl(url), projectDir, Long.parseLong(revision));
} catch (MalformedURLException ex) {
LOG.log(Level.WARNING, null, ex);
}
}
}
}
});
}
private void addCreditsPane() throws IOException {
String creditsText = getCreditsTextAsHtml();
JTextPane textPane = JTextPaneFactory.createHmtlJTextPane(imageInfoReader);
textPane.setEditable(false);
textPane.setText(creditsText);
textPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
openWebPage(e.getURL().toURI());
} catch (URISyntaxException e1) {
throw new IllegalStateException("Problem opening " + e.getURL().toString(), e1);
}
}
}
});
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setBounds(15, 15, 668, 720);
addComponent(scrollPane);
}
public static JTextPane createHtmlPanel() {
JTextPane htmlPanel = new JTextPane();
htmlPanel.setEditable(false);
htmlPanel.setContentType("text/html");
htmlPanel.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == EventType.ACTIVATED) {
try {
BrowserLauncher.openURL(e.getURL().toString());
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
});
return htmlPanel;
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(MainWin.icon);
setSize(500, 300);
setResizable(false);
setLocationRelativeTo(this.getParent());
setTitle("About " + AppConfig.appTitle);
JPanel mainPanel = new JPanel(new BorderLayout());
add(mainPanel);
JPanel leftPanel = new JPanel();
mainPanel.add(leftPanel, BorderLayout.WEST);
ImageIcon iconImg = new ImageIcon(MainWin.icon);
iconImg.setImage(iconImg.getImage().getScaledInstance((int) (iconImg.getIconWidth() * 0.5), (int) (iconImg.getIconHeight() * 0.5), Image.SCALE_SMOOTH));
JLabel iconLabel = new JLabel(iconImg);
iconLabel.setBorder(BorderFactory.createEmptyBorder(6, 10, 6, 10));
leftPanel.add(iconLabel);
HyperlinkListener hlLsnr = new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
return;
// 超链接标记中必须带有协议指定,e.getURL()才能得到,否则只能用e.getDescription()得到href的内容。
// JOptionPane.showMessageDialog(InfoDialog.this, "URL:"+e.getURL()+"\nDesc:"+ e.getDescription());
URL linkUrl = e.getURL();
if (linkUrl != null) {
try {
Desktop.getDesktop().browse(linkUrl.toURI());
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "无法打开超链接:" + linkUrl + "\n详情:" + e1, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "超链接信息不完整:" + e.getDescription() + "\n请确保链接带有协议信息,如http://,mailto:", JOptionPane.ERROR_MESSAGE);
}
}
};
JTextPane infoArea = new JTextPane();
//设置css单位(px/pt)和chrome一致
infoArea.putClientProperty(JTextPane.W3C_LENGTH_UNITS, true);
infoArea.addHyperlinkListener(hlLsnr);
infoArea.setContentType("text/html");
infoArea.setText(getInfo());
infoArea.setEditable(false);
infoArea.setBorder(BorderFactory.createEmptyBorder(2, 10, 6, 10));
infoArea.setFocusable(false);
JScrollPane infoAreaScrollPane = new JScrollPane(infoArea);
infoAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
infoAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
infoAreaScrollPane.getViewport().setBackground(Color.WHITE);
mainPanel.add(infoAreaScrollPane, BorderLayout.CENTER);
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(MainWin.icon);
setSize(500, 300);
setResizable(false);
setLocationRelativeTo(this.getParent());
setTitle("About " + AppConfig.appTitle);
JPanel mainPanel = new JPanel(new BorderLayout());
add(mainPanel);
JPanel leftPanel = new JPanel();
mainPanel.add(leftPanel, BorderLayout.WEST);
ImageIcon iconImg = new ImageIcon(MainWin.icon);
iconImg.setImage(iconImg.getImage().getScaledInstance((int) (iconImg.getIconWidth() * 0.5), (int) (iconImg.getIconHeight() * 0.5), Image.SCALE_SMOOTH));
JLabel iconLabel = new JLabel(iconImg);
iconLabel.setBorder(BorderFactory.createEmptyBorder(6, 10, 6, 10));
leftPanel.add(iconLabel);
HyperlinkListener hlLsnr = new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
return;
// 超链接标记中必须带有协议指定,e.getURL()才能得到,否则只能用e.getDescription()得到href的内容。
// JOptionPane.showMessageDialog(InfoDialog.this, "URL:"+e.getURL()+"\nDesc:"+ e.getDescription());
URL linkUrl = e.getURL();
if (linkUrl != null) {
try {
Desktop.getDesktop().browse(linkUrl.toURI());
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "无法打开超链接:" + linkUrl + "\n详情:" + e1, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "超链接信息不完整:" + e.getDescription() + "\n请确保链接带有协议信息,如http://,mailto:", JOptionPane.ERROR_MESSAGE);
}
}
};
JTextPane infoArea = new JTextPane();
//设置css单位(px/pt)和chrome一致
infoArea.putClientProperty(JTextPane.W3C_LENGTH_UNITS, true);
infoArea.addHyperlinkListener(hlLsnr);
infoArea.setContentType("text/html");
infoArea.setText(getInfo());
infoArea.setEditable(false);
infoArea.setBorder(BorderFactory.createEmptyBorder(2, 10, 6, 10));
infoArea.setFocusable(false);
JScrollPane infoAreaScrollPane = new JScrollPane(infoArea);
infoAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
infoAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
infoAreaScrollPane.getViewport().setBackground(Color.WHITE);
mainPanel.add(infoAreaScrollPane, BorderLayout.CENTER);
}
/**
* This method displays the about box.
*/
public Runner doAbout() {
if (wrap)
return wrapMe();
// Old about message
// OurDialog.showmsg("About Alloy Analyzer " + Version.version(), OurUtil.loadIcon("images/logo.gif"), "Alloy Analyzer " + Version.version(), "Build date: " + " git: " + Version.commit, " ", "Lead developer: Felix Chang", "Engine developer: Emina Torlak", "Graphic design: Julie Pelaez", "Project lead: Daniel Jackson", " ", "Please post comments and questions to the Alloy Community Forum at http://alloy.mit.edu/", " ", "Thanks to: Ilya Shlyakhter, Manu Sridharan, Derek Rayside, Jonathan Edwards, Gregory Dennis,", "Robert Seater, Edmond Lau, Vincent Yeung, Sam Daitch, Andrew Yip, Jongmin Baek, Ning Song,", "Arturo Arizpe, Li-kuo (Brian) Lin, Joseph Cohen, Jesse Pavel, Ian Schechter, and Uriel Schafer.");
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000; font-family:Verdana, Trebuchet MS,Geneva, sans-serif; font-size: 10px; margin: 4px; }");
styleSheet.addRule("h1 {color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color: #C0C0C0; padding: 4px; margin: 4px; }");
styleSheet.addRule("th {text-align:left;}");
JTextPane ta = new JTextPane();
ta.setEditorKit(kit);
ta.setContentType("text/html");
ta.setBackground(null);
ta.setBorder(null);
ta.setFont(new JLabel().getFont());
// @formatter:off
ta.setText("<html><h1>Alloy Analyzer " + Version.getShortversion() + "</h1>"
+ "<br/>"
+ "<html>"
+ "<tr><th>Project Lead</th><td>Daniel Jackson</td></tr>"
+ "<tr><th>Chief Developer</th><td>Aleksandar Milicevic</td></tr>"
+ "<tr><th>Kodkod Engine</th><td>Emina Torlak</td></tr>"
+ "<tr><th>Open Source</th><td>Peter Kriens</td></tr>"
+ "</table><br/>"
+ "<p>For more information about Alloy, <a href='http://alloytools.org'>http://alloytools.org</a></p>"
+ "<p>Questions and comments about Alloy are welcome at the community forum:</p>"
+ "<p>Alloy Community Forum: <a href='https://groups.google.com/forum/#!forum/alloytools'>https://groups.google.com/forum/#!forum/alloytools</a></p>"
+ "<p>Alloy experts also respond to <a href='https://stackoverflow.com/questions/tagged/alloy'>https://stackoverflow.com</a> questions tagged <code>alloy</code>.</p>"
+ "<p>Major contributions to earlier versions of Alloy were made by: Felix Chang (v4);<br/>"
+ "Jonathan Edwards, Eunsuk Kang, Joe Near, Robert Seater, Derek Rayside, Greg Dennis,<br/>"
+ "Ilya Shlyakhter, Mana Taghdiri, Mandana Vaziri, Sarfraz Khurshid (v3); Manu Sridharan<br/>"
+ "(v2); Edmond Lau, Vincent Yeung, Sam Daitch, Andrew Yip, Jongmin Baek, Ning Song,<br/>"
+ "Arturo Arizpe, Li-kuo (Brian) Lin, Joseph Cohen, Jesse Pavel, Ian Schechter, Uriel<br/>"
+ "Schafer (v1).</p>"
+ "<p>The development of Alloy was funded by part by the National Science Foundation under<br/>"
+ "Grant Nos. 0325283, 0541183, 0438897 and 0707612; by the Air Force Research Laboratory<br/>"
+ "(AFRL/IF) and the Disruptive Technology Office (DTO) in the National Intelligence<br/>"
+ "Community Information Assurance Research (NICIAR) Program; and by the Nokia<br/>"
+ "Corporation as part of a collaboration between Nokia Research and MIT CSAIL.</p>"
+ "<br/><pre>"
+ "Build Date: " + Version.buildDate() + "<br/>"
+ "Git Commit: " + Version.commit
+ "</pre>");
// @formatter:on
ta.setEditable(false);
ta.addHyperlinkListener((e) -> {
if (e.getEventType() == EventType.ACTIVATED) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException | URISyntaxException e1) {
// ignore
}
}
}
});
OurDialog.showmsg("About Alloy Analyzer " + Version.version(), ta);
return null;
}
JHTML(String s)
{
super(new BorderLayout());
html = new JTextPane();
html.setEditable(false); // need to set this explicitly to fix swing 1.3 bug
html.setCaretPosition(0);
html.setContentType("text/html");
// Enable posting of form submit events to the hyper link listener
final HTMLEditorKit htmlEditor
= (HTMLEditorKit)html.getEditorKitForContentType("text/html");
try {
// Call htmlEditor.setAutoFormSubmission(false); if available (Java 5+)
final Method setAutoFormSubmissionMethod = htmlEditor.getClass()
.getMethod("setAutoFormSubmission", new Class[]{ Boolean.TYPE});
setAutoFormSubmissionMethod.invoke(htmlEditor,
new Object[]{Boolean.FALSE});
} catch (final Throwable t) {
Activator.log.warn("Failed to enable auto form submission for JHTMLBundle.", t);
}
html.setText(s);
html.setCaretPosition(0);
html.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent ev)
{
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
final URL url = ev.getURL();
try {
if (Util.isBundleLink(url)) {
final long bid = Util.bidFromURL(url);
Activator.disp.getBundleSelectionModel().clearSelection();
Activator.disp.getBundleSelectionModel().setSelected(bid, true);
} else if (Util.isImportLink(url)) {
JCMInfo.importCfg(JHTML.this);
} else {
Util.openExternalURL(url);
}
} catch (final Exception e) {
Activator.log.error("Failed to show " + url, e);
}
}
}
});
scroll =
new JScrollPane(html, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
html.setPreferredSize(new Dimension(300, 300));
add(scroll, BorderLayout.CENTER);
}
private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
String version = MainFrame.class.getPackage().getImplementationVersion();
if (version == null) {
version = "unknown";
}
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setContentType("text/html");
textPane.setText(
"<font face=sans-serif><strong><font size=+2>ProCamCalib</font></strong> version " + version + "<br>" +
"Copyright (C) 2009-2014 Samuel Audet <<a href=\"mailto:[email protected]%28Samuel%20Audet%29\">[email protected]</a>><br>" +
"Web site: <a href=\"http://www.ok.ctrl.titech.ac.jp/~saudet/procamcalib/\">http://www.ok.ctrl.titech.ac.jp/~saudet/procamcalib/</a><br>" +
"<br>" +
"Licensed under the GNU General Public License version 2 (GPLv2).<br>" +
"Please refer to LICENSE.txt or <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a> for details."
);
textPane.setCaretPosition(0);
Dimension dim = textPane.getPreferredSize();
dim.height = dim.width*3/4;
textPane.setPreferredSize(dim);
textPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == EventType.ACTIVATED) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch(Exception ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE,
"Could not launch browser to \"" + e.getURL()+ "\"", ex);
}
}
}
});
// pass the scrollpane to the joptionpane.
JDialog dialog = new JOptionPane(textPane, JOptionPane.PLAIN_MESSAGE).
createDialog(this, "About");
if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(UIManager.getLookAndFeel().getClass().getName())) {
// under GTK, frameBackground is white, but rootPane color is OK...
// but under Windows, the rootPane's color is funny...
Color c = dialog.getRootPane().getBackground();
textPane.setBackground(new Color(c.getRGB()));
} else {
Color frameBackground = this.getBackground();
textPane.setBackground(frameBackground);
}
dialog.setVisible(true);
}
private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
String version = MainFrame.class.getPackage().getImplementationVersion();
if (version == null) {
version = "unknown";
}
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setContentType("text/html");
textPane.setText(
"<font face=sans-serif><strong><font size=+2>ProCamTracker</font></strong> version " + version + "<br>" +
"Copyright (C) 2009-2014 Samuel Audet <<a href=\"mailto:[email protected]%28Samuel%20Audet%29\">[email protected]</a>><br>" +
"Web site: <a href=\"http://www.ok.ctrl.titech.ac.jp/~saudet/procamtracker/\">http://www.ok.ctrl.titech.ac.jp/~saudet/procamtracker/</a><br>" +
"<br>" +
"Licensed under the GNU General Public License version 2 (GPLv2).<br>" +
"Please refer to LICENSE.txt or <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a> for details."
);
textPane.setCaretPosition(0);
Dimension dim = textPane.getPreferredSize();
dim.height = dim.width*3/4;
textPane.setPreferredSize(dim);
textPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == EventType.ACTIVATED) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch(Exception ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE,
"Could not launch browser to \"" + e.getURL()+ "\"", ex);
}
}
}
});
// pass the scrollpane to the joptionpane.
JDialog dialog = new JOptionPane(textPane, JOptionPane.PLAIN_MESSAGE).
createDialog(this, "About");
if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(UIManager.getLookAndFeel().getClass().getName())) {
// under GTK, frameBackground is white, but rootPane color is OK...
// but under Windows, the rootPane's color is funny...
Color c = dialog.getRootPane().getBackground();
textPane.setBackground(new Color(c.getRGB()));
} else {
Color frameBackground = this.getBackground();
textPane.setBackground(frameBackground);
}
dialog.setVisible(true);
}