2012-12-26 104 views
1

我有一个带有StyledDocument“doc”的JTextPane,当我想要JTextPane显示一个可以单击的超链接时。我把“http://www.google.com”和attrs作为例子,因为我真的不知道如何去做。令人惊讶的是,我找不到任何有用的信息(没有html或HTMLDocument等)。我不喜欢swing如何处理html,我不喜欢使用它。如何将超链接添加到没有html的JTextPane?

public class SSCCE extends JFrame { 

    private JPanel contentPane; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        SSCCE frame = new SSCCE(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
    public SSCCE() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 

     JTextPane textPane = new JTextPane(); 
     textPane.setEditable(false); 
     StyledDocument doc = textPane.getStyledDocument(); 
     SimpleAttributeSet attrs = new SimpleAttributeSet(); 
     try { 
      doc.insertString(doc.getLength(), "http://www.google.com", attrs); 
     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
     GroupLayout gl_contentPane = new GroupLayout(contentPane); 
     gl_contentPane.setHorizontalGroup(
      gl_contentPane.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_contentPane.createSequentialGroup() 
        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 422, GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     gl_contentPane.setVerticalGroup(
      gl_contentPane.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_contentPane.createSequentialGroup() 
        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     contentPane.setLayout(gl_contentPane); 
    } 
} 

回答

4

定义您自己的AttributeSet以保持超链接信息。它应该包括例如蓝色和自定义属性。我们将其命名为“URL”。将一些文字与AttributeSet添加到StyledDocument

然后添加一个鼠标监听器(Motion和Mouse监听器)。对于任何鼠标事件,您可以使用viewToModel()来获得指定鼠标位置的偏移量。获取偏移量的叶子元素(文本),并检查文本Element是否具有属性。

如果它执行了您的操作(例如将鼠标光标设置为手动或处理,请单击该URL)。

4

+1 to StanislavL answer.Here is simple approach using HTML。 好吧,在你的java代码中使用HTML标签并不复杂。其他方式可能很复杂。我做了一个简短的EG,易于执行和理解,你可以修改它并使其适合你的目的。

enter image description here

CODE:

import java.awt.Cursor; 
import java.awt.Desktop; 
import java.awt.EventQueue; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 


public class JlabelLink extends JFrame { 
private JPanel pan; 

    private JLabel website; 

public JlabelLink() { 
    this.setTitle("jLabelLinkExample"); 
    this.setSize(300, 100); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 

    pan = new JPanel(); 

    website = new JLabel(); 


    website.setText("<html> Website : <a href=\"\">http://www.google.com/</a></html>"); 
    website.setCursor(new Cursor(Cursor.HAND_CURSOR)); 


    pan.add(website); 
    this.setContentPane(pan); 
    this.setVisible(true); 

    goWebsite(website); 
} 


    public static void main(String args[]) { 

    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new JlabelLink().setVisible(true); 
     } 
    }); 
    } 

    private void goWebsite(JLabel website) { 
    website.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      try { 
       try { 
        Desktop.getDesktop().browse(new URI("http://www.google.com/webhp?nomo=1&hl=fr")); 
       } catch (IOException ex) { 
        Logger.getLogger(JlabelLink.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
      catch (URISyntaxException ex) { 

      } 
     } 
     }); 
    } 
    }