想输出一个带有超链接的字符串到jTextarea里,然后点击这个超链接会触发某个事件这个怎么写?

解决方案 »

  1.   

    JEditorPane支持简单的网页,它的子类JTextPane能像QQ一样加入JLabel、JButton等组件,可以设置文字显示不同的字体、颜色等。
      

  2.   

    http://www.jroller.com/page/santhosh?entry=binding_hyperlinks_to_actions
      

  3.   

    这个是怎么触发超链接事件吧..但 我现在要输出 带有 超链接的 文字 ,难道就要按html格式输出 ?但我 写的是一个简单 聊天程序..我想双方 发送的文字带上 超链接 ..这 怎么做
      

  4.   

    以前写的个JTextPane例子,可以参考下:import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.text.*;
    import java.io.*;public class Test {
      JFrame frame;
      JTextPane textPane;
      File file;
      Icon image;  public Test(){
        frame = new JFrame("JTextPane");
        textPane = new JTextPane();
        file = new File("./classes/test/icon.gif");
        image = new ImageIcon(file.getAbsoluteFile().toString());
      }  public void insert(String str, AttributeSet attrSet) {
        Document doc = textPane.getDocument();
        str ="\n" + str ;
        try {
          doc.insertString(doc.getLength(), str, attrSet);
        }
        catch (BadLocationException e) {
          System.out.println("BadLocationException: " + e);
        }
      }  public void setDocs(String str,Color col,boolean bold,int fontSize) {
        SimpleAttributeSet attrSet = new SimpleAttributeSet();
        StyleConstants.setForeground(attrSet, col);
        //颜色
        if(bold==true){
          StyleConstants.setBold(attrSet, true);
        }//字体类型
        StyleConstants.setFontSize(attrSet, fontSize);
        //字体大小
        insert(str, attrSet);
      }  public void gui() {
        textPane.insertIcon(image);
        setDocs("第一行的文字",Color.red,false,20);
        setDocs("第二行的文字",Color.BLACK,true,25);
        setDocs("第三行的文字",Color.BLUE,false,20);
        frame.getContentPane().add(textPane, BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }});
        frame.setSize(200,300);
        frame.setVisible(true);
      }
      public static void main(String[] args) {
        Test test = new Test();
        test.gui();
      }
    }
      

  5.   

    我给你代码吧:
    全部4个类:一个HTML文件:
    HyperLinksDemo.JAVA
    package html;import javax.swing.ActionMap;
    import javax.swing.BorderFactory;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;//@author Santhosh Kumar T - [email protected] 
    public class HyperLinksDemo extends JFrame{ 
        public HyperLinksDemo() throws Exception{ 
            super("Hyperlinks Demo - [email protected]"); 
            JEditorPane editor = new JEditorPane(); 
            editor.setPage(getClass().getResource("index.html")); 
            editor.setEditable(false); 
     
            ActionMap actionMap = new ActionMap(); 
            actionMap.put("selectPeople", new SelectPeopleAction()); 
            editor.addHyperlinkListener(new ActionBasedHyperlinkListener(actionMap)); 
     
            JPanel contents = (JPanel)getContentPane(); 
            contents.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
            contents.add(new JScrollPane(editor)); 
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            setSize(1000, 600); 
        } 
     
        public static void main(String[] args) throws Exception{ 
            new HyperLinksDemo().setVisible(true); 
        } 
    }
    ActionBasedHyperlinkListener.JAVA
    package html;import java.awt.event.ActionEvent;import javax.swing.Action;
    import javax.swing.ActionMap;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;//@author Santhosh Kumar T - [email protected] 
    public class ActionBasedHyperlinkListener implements HyperlinkListener{ 
        ActionMap actionMap; 
     
        public ActionBasedHyperlinkListener(ActionMap actionMap){ 
            this.actionMap = actionMap; 
        } 
     
        public void hyperlinkUpdate(HyperlinkEvent e){ 
            if(e.getEventType()!=HyperlinkEvent.EventType.ACTIVATED) 
                return; 
            String href = e.getDescription(); 
            Action action = actionMap.get(href); 
            if(action!=null) 
                action.actionPerformed(new ActionEvent(e, ActionEvent.ACTION_PERFORMED, href)); 
        } 
    }  HyperlinkActivator.JAVA
    package html;import javax.swing.JEditorPane;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;
    public class HyperlinkActivator implements HyperlinkListener{ 
        public void hyperlinkUpdate(HyperlinkEvent e){ 
            if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ 
                try{ 
                    ((JEditorPane)e.getSource()).setPage(e.getURL()); 
                } catch(Exception ex){ 
                    ex.printStackTrace(); 
                } 
            } 
        }
    }
      

  6.   

    SelectPeopleAction.JAVApackage html;import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.util.StringTokenizer;import javax.swing.AbstractAction;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.Element;//@author Santhosh Kumar T - [email protected] 
    public class SelectPeopleAction extends AbstractAction{ 
        private final static String people[] = {"Santhosh", "Rick", "Matt", "Lorimer", "Scott"}; 
     
        public SelectPeopleAction(){ 
            super("selectPeople"); 
        } 
     
        public void actionPerformed(ActionEvent e){ 
            HyperlinkEvent hle = (HyperlinkEvent)e.getSource(); 
     
            try{ 
                Element elem = hle.getSourceElement(); 
                Document doc = elem.getDocument(); 
                int start = elem.getStartOffset(); 
                int end = elem.getEndOffset(); 
                String link = doc.getText(start, end-start); 
                link = link.equals("contains people") ? "" : link.substring("contains ".length()); 
     
                JList list = new JList(people); 
                StringTokenizer stok = new StringTokenizer(link, ", "); 
                while(stok.hasMoreTokens()){ 
                    String token = stok.nextToken(); 
                    for(int i = 0; i<list.getModel().getSize(); i++){ 
                        if(list.getModel().getElementAt(i).equals(token)) 
                            list.getSelectionModel().addSelectionInterval(i, i); 
                    } 
                } 
     
                int response = JOptionPane.showOptionDialog((Component)hle.getSource(), new JScrollPane(list), "People", 
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, 
                        null, null, null); 
                if(response==JOptionPane.OK_OPTION){ 
                    String newLink = ""; 
                    Object selected[] = list.getSelectedValues(); 
                    for(int i = 0; i<selected.length; i++){ 
                        if(i!=0) 
                            newLink += ", "; 
                        newLink += selected[i]; 
                    } 
                    newLink = newLink.length()==0 ? "contains people" : "contains "+newLink; 
                    elem.getDocument().remove(start, end-start); 
                    elem.getDocument().insertString(start, newLink, elem.getAttributes()); 
                } 
            } catch(BadLocationException ex){ 
                ex.printStackTrace(); 
            } 
        } 
    } INDEX.HTML
    <html> 
    <body> 
     
    <p>When the newly arrived message arrives<br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where the From <a href="selectPeople"> 
    contains people</a><br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete it.</p> 
     
    </body> 
     
    </html>