Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing JDialog/JTextPane和HTML链接_Java_Html_Swing_User Interface_Jtextpane - Fatal编程技术网

Java Swing JDialog/JTextPane和HTML链接

Java Swing JDialog/JTextPane和HTML链接,java,html,swing,user-interface,jtextpane,Java,Html,Swing,User Interface,Jtextpane,我在JDialog中的swingJTextPane中使用html页面。 在html中,我有一个 当我通过浏览器查看网页时,当鼠标指向链接时,我可以看到mailto 当我按下链接时,会出现错误“未安装默认邮件客户端”,但我想这是因为我的电脑没有配置Outlook或其他程序。 当我从Swing应用程序打开JDialog时,我看到John突出显示为链接,但当我按下链接时,什么也没有发生。 我希望收到与浏览器相同的错误消息。 所以我的问题是,链接是否可以通过Swing应用程序打开 感谢无论是工具提示(显

我在JDialog中的swing
JTextPane
中使用html页面。
在html中,我有一个

当我通过浏览器查看网页时,当鼠标指向链接时,我可以看到
mailto

当我按下链接时,会出现错误“未安装默认邮件客户端”,但我想这是因为我的电脑没有配置Outlook或其他程序。
当我从Swing应用程序打开JDialog时,我看到
John
突出显示为链接,但当我按下链接时,什么也没有发生。
我希望收到与浏览器相同的错误消息。
所以我的问题是,链接是否可以通过Swing应用程序打开

感谢

无论是工具提示(显示目标超链接地址)还是按键操作都不会自动发生,您必须对其进行编码:对于第一种情况,请使用ToolTimManager注册窗格,对于第二种情况,请注册HyperlinkListener,类似于:

    final JEditorPane pane = new JEditorPane("http://swingx.java.net");
    pane.setEditable(false);
    ToolTipManager.sharedInstance().registerComponent(pane);

    HyperlinkListener l = new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
                try {
                    pane.setPage(e.getURL());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        }

    };
    pane.addHyperlinkListener(l);
示例是在同一窗格中打开页面。如果要激活默认浏览器/邮件客户端,请让桌面(jdk1.6的新版本)为您执行此操作

final JEditorPane jep=new JEditorPane(“text/html”,
final JEditorPane jep = new JEditorPane("text/html",
    "The rain in <a href='http://foo.com/'>Spain</a> falls mainly on the <a href='http://bar.com/'>plain</a>.");

jep.setEditable(false);
jep.setOpaque(false);
final Desktop desktop = Desktop.getDesktop(); 

jep.addHyperlinkListener(new HyperlinkListener() {

    public void hyperlinkUpdate(HyperlinkEvent hle) {
        if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
            try {
                System.out.println(hle.getURL());
                jep.setPage(hle.getURL());
                try {
                    desktop.browse(new URI(hle.getURL().toString()));
                } catch (URISyntaxException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (IOException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
});

JPanel p = new JPanel();
p.add(new JLabel("Foo."));
p.add(jep);
p.add(new JLabel("Bar."));

JFrame f = new JFrame("HyperlinkListener");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(400, 150);
f.setVisible(true);
“年的雨主要落在树上。”); jep.setEditable(false); jep.setOpaque(假); 最终桌面=Desktop.getDesktop(); jep.addHyperlinkListener(新的HyperlinkListener(){ 公共无效hyperlinkUpdate(HyperlinkEvent hle){ if(HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())){ 试一试{ System.out.println(hle.getURL()); setPage(hle.getURL()); 试一试{ 浏览(新URI(hle.getURL().toString()); }捕获(URISyntaxException-ex){ Logger.getLogger(App.class.getName()).log(Level.SEVERE,null,ex); } }捕获(IOEX异常){ Logger.getLogger(App.class.getName()).log(Level.SEVERE,null,ex); } } } }); JPanel p=新的JPanel(); p、 添加(新JLabel(“Foo”); p、 添加(jep); p、 添加(新的JLabel(“Bar”); JFrame f=新JFrame(“HyperlinkListener”); f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f、 getContentPane().add(p,BorderLayout.CENTER); f、 设置大小(400150); f、 setVisible(真);
您可能想解释一下您的答案,因为OPnice帮助了我!