2

プログラムの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

3 に答える 3

3

いつものように追加MouseListenerして右クリックで聞く。クリックviewToModel()時に のメソッドを使用JEditorPaneして、ドキュメント内のオフセットを取得します。次に、getCharacterElement()のメソッドを使用StyledDocumentしてリーフ要素を取得するかどうかを確認します。次に、リーフがハイパーリンクかどうかを確認します。

または、このhttp://java-sl.com/tip_links_in_editable.htmlを使用して独自の実装を行うことができますLinkController

于 2012-10-17T10:39:44.160 に答える