0

嗨,我正在尝试向用户展示一个简单的 Jframe,用户可以在其中看到前 10 名 MMO 的图像。我已经有一个简单的 html 文件,其中的图像将用户带到正确的游戏 Web URL。

    public static void main(String[] args) throws Exception {
    String url = "exmaple web address etc http//..";
    JEditorPane editor = new JEditorPane(url);
    editor.setEditable(false);
    JScrollPane pane = new JScrollPane(editor);
    JFrame frame = new JFrame("Top 10 MMO's");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane);
    frame.setSize(400, 300);
    frame.setVisible(true);

    public void HyperLinked(HyperlinkEvent e) throws URISyntaxException {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(e.getURL().toURI());
            } catch (IOException | URISyntaxException e1) {
            }
        }
    }
}

示例 html 代码包含图像,每个图像都有自己的可点击网址

            </p>
            <p>
                <h4>Dungeons and Dragons</h4>
                <a href="http://www.ddo.com/" target="_blank"><img src="images/ddo.jpg" alt="Dungeons and Dragons Online" width="351" height="144" border="0" /></a>
            </p>
            <p>
                <h4>Lord of the Rings</h4>
                <a href="http://www.lotro.com/en?lang=en_GB&" target="_blank"><img src="images/lotro.jpg" alt="Lord of the Rings" width="351" height="144" border="0" /></a>
            </p>
            <p>
                <h4>Aion</h4>
                <a href="http://www.aionfreetoplay.com/website/" target="_blank"><img src="images/aion.jpg" alt="Aion" width="351" height="144" border="0" /></a>
            </p>
            <p>

下面的图片示例不允许发布为新用户规则等 http://s14.postimage.org/7xt52lbox/mmoset.jpg

我能够显示图像,并且我知道它们是可点击的并且仅在 JFrame 中工作我无法点击它们,想知道我是否需要添加鼠标点击或其他东西?

我会很感激反馈或一些帮助,谢谢!

4

1 回答 1

0

你几乎明白了。

    editor.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                if (Desktop.isDesktopSupported()) {
                    try {
                        Desktop.getDesktop().browse(e.getURL().toURI());
                    } catch (IOException | URISyntaxException e1) {
                    }
                }
            }
        }
    });
于 2013-01-28T16:05:34.680 回答