JOptionPane 中 JEditorPane 内部的超链接抛出 NullPointerException

Hyperlink inside of a JEditorPane in a JOptionPane throwing a NullPointerException

我带着另一个问题回来了。我之前在 Java 中没有对 URL 和 hyperlink 做过任何事情,对 html 标记的经验也很少。我已经阅读了教程和很多 SO 问题,但似乎无法弄清楚为什么我会在这里抛出异常。

所以我有一个程序,我添加了一个 "About" 按钮。单击它会打开一个 JOptionPane。在 JOptionPane 中,我有一个 JEditorPane,里面有一大堆文本,最后有两个 hyperlinks,用于我用作源的网站。现在我添加了 links 并且它们是可点击的,但是当我点击它们时,它会抛出 NullPointerException。我确信它与 ActionListener 有关,我尝试了几种不同的实现方式,但结果相同。

我希望发生的情况是,当单击 link 时,它会打开默认浏览器并导航到该站点。这是我为尝试调试而编写的测试程序,但包含与我的主要代码相同的代码。

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class HyperlinkTest extends JFrame {
    private static final long serialVersionUID = 1L;

    JPanel panel = new JPanel();
    JButton aboutButton = new JButton("About");

    public void aboutPopUp() throws IOException {

        final JEditorPane ep = new JEditorPane();

        ep.setEditable(false);
        ep.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));

        ep.setText("<html>Blah Blah<br />"
                + "Blah Blah<br />"
                + "Blah: <a href=\\"\\">http://exploration.grc.nasa.gov/education/rocket/rktcg.html</a><br />"
                + "Blah: <a href=\\"\\">http://exploration.grc.nasa.gov/education/rocket/rktcp.html</a></html>");

        // handle link events
        ep.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
                    if (Desktop.isDesktopSupported()) {
                        try {
                            Desktop.getDesktop().browse(e.getURL().toURI());
                        } catch (IOException | URISyntaxException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });

        JOptionPane.showMessageDialog(null, ep, "How To Use", JOptionPane.PLAIN_MESSAGE);
    }    

    private void aboutButtonActionPerformed(ActionEvent ab) throws IOException {

        aboutPopUp();
    }

    private void initComponents() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(panel);
        panel.add(aboutButton);
        aboutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ab) {
                try {
                    aboutButtonActionPerformed(ab);
                } catch (IOException ex) {
                    Logger.getLogger(HyperlinkTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        pack();
        setLocationRelativeTo(null);
    }

    public HyperlinkTest() {
        initComponents();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HyperlinkTest().setVisible(true);
            }
        });
    } 
}

提前感谢你们提供的任何帮助,你们都很棒!!

你有

ep.setText("<html>Blah Blah<br />"
            + "Blah Blah<br />"
            + "Blah: <a     href=\\"\\">http://exploration.grc.nasa.gov/education/rocket/rktcg.html</a><br />"
            + "Blah: <a href=\\"\\">http://exploration.grc.nasa.gov/education/rocket/rktcp.html</a></html>");

我想你想要的是

ep.setText("<html>Blah Blah<br />"
            + "Blah Blah<br />"
            + "Blah: <a href=\"http://exploration.grc.nasa.gov/education/rocket/rktcg.html\">http://exploration.grc.nasa.gov/education/rocket/rktcg.html</a><br />"
            + "Blah: <a href=\"http://exploration.grc.nasa.gov/education/rocket/rktcp.html\">http://exploration.grc.nasa.gov/education/rocket/rktcp.html</a></html>");

请注意我没有编译这个。您可能需要转义 better/differently.

此外,您可能想看看 this