0

可能重复:
处理 JTextPane 上的超链接右键单击

这可能是一个简单的问题,但我似乎无法正确完成!单击超链接时如何触发上下文弹出菜单?

public class rchltest extends Applet {

    public void init() {

        JPanel panel = new JPanel(false);

        JEditorPane gentextp = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(gentextp);
        panel.add(scrollPane);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        gentextp.setContentType("text/html");
        gentextp.setEditable(false);
        gentextp.addHyperlinkListener(new texthll());
        gentextp.setPreferredSize( new Dimension( 500, 400 ) );
        gentextp.setText("Here is a <a href='http://A'>hyperlink</a>");

        this.add( panel );

    }
}

class texthll implements HyperlinkListener  {

        public void hyperlinkUpdate(HyperlinkEvent event) {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            JEditorPane pane = (JEditorPane)event.getSource();

            URL url = event.getURL();

                // Show the new page in the editor pane.
                JOptionPane.showMessageDialog( null, url);
        }
    }
}
4

0 回答 0