【问题标题】:Clickable text in a JTextPaneJTextPane 中的可点击文本
【发布时间】:2013-04-21 13:47:04
【问题描述】:

我有一个 JTextPane 声明如下:

JTextPane box = new JTextPane();
JScrollPane scroll = new JScrollPane();
StyledDocument doc = box.getStyledDocument();
scroll.setViewportView(box);
scroll = new JScrollPane(box);

我在其上附加如下文本:

public void appendChatText(String text)
{   
    try
    {
        doc.insertString(doc.getLength(), text, null);
        box.setAutoscrolls(true);
        box.setCaretPosition(box.getDocument().getLength());    
    }
    catch(BadLocationException e)
    {
        e.printStackTrace();
    }
}

我还设法轻松地让 JTextPane 根据需要显示图像和组件,但我不知道如何将可点击文本编码到 JTextPane 中。例如,我希望它打印一条消息,上面写着“文件已上传到服务器。Accept *Decline*”,如果用户单击接受或拒绝字符串,则它会执行相应的功能.关于如何有效实现这一点的任何想法?

【问题讨论】:

    标签: java swing url jtextpane


    【解决方案1】:

    我最终通过一个 MouseListener 和一个扩展 AsbstractAction 的类解决了这个问题。我添加了我想成为 JTextPane 的可点击链接的文本,如下所示:

    `Style regularBlue = doc.addStyle("regularBlue", StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
     StyleConstants.setForeground(regularBlue, Color.BLUE);
     StyleConstants.setUnderline(regularBlue, true);
     regularBlue.addAttribute("linkact", new ChatLinkListener(textLink));
     doc.insertString(doc.getLength(), textLink, regularBlue);`
    

    我在其他地方初始化了 JTextPane 上的 MouseListener,并将以下代码添加到我的侦听器类中:

    public void mouseClicked(MouseEvent e)
            {
                Element ele = doc.getCharacterElement(chatbox.viewToModel(e.getPoint()));
                AttributeSet as = ele.getAttributes();
                ChatLinkListener fla = (ChatLinkListener)as.getAttribute("linkact");
                if(fla != null)
                {
                    fla.execute();
                }
            }
    

    最后,这引用了实际执行操作的类:

    class ChatLinkListener extends AbstractAction
        {
            private String textLink;
    
            ChatLinkListener(String textLink)
            {
                this.textLink = textLink;
            }
    
            protected void execute()
            {
                if("accept".equals(url))
                {
                    //execute code
                }
                else if("decline".equals(url))
                {
                    //execute code
                }
            }
    
            public void actionPerformed(ActionEvent e)
            {
                execute();
            }
        }
    

    【讨论】:

      【解决方案2】:

      我还设法轻松地让 JTextPane 根据需要显示图像和组件 ...我希望它打印一条消息,上面写着“文件上传到服务器。接受拒绝

      为什么不添加“接受”和“拒绝”按钮?

      否则,您可以使用 JEditorPane 来显示 HTML。然后,您可以将 HyperlinkListener 添加到“Accept”和“Decline”文本中。阅读 JEditorPane API 以获取示例。当您单击文本时,HyperlinkListener 需要一个 URL,但我认为没有任何理由它不能只是一个字符串。

      【讨论】:

      • 谢谢,我过几天有空就试试这个。如果问题解决了,我会接受答案。个性试图避免使用按钮来保持它看起来整洁干净,所以 HyperLinkListener 听起来像是我要走的路。 :)
      【解决方案3】:

      如果您不喜欢使用按钮,请使用 JLabel。

      如果您使用的是 JTextPane,您可以使用 insertComponent() 方法插入与 JTextPane 字体具有相同字体的新 JLabel,并且您可以按照您想要的方式自定义 JLabel,例如将光标设置为手形光标使其具有可点击的外观和感觉。

      JLabel l=new JLabel("Click me");
      l.setFont(textPane.getFont());
      l.setCursor(new Cursor(Cursor.HAND_CURSOR));
      l.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent me)
         {
               // your code
         }
      });
      textPane.insertComponent(l);
      

      【讨论】:

        【解决方案4】:

        mouselistener 添加到您希望可点击的文本并执行适当的操作。

        【讨论】:

          【解决方案5】:

          我认为它是您想要的“JOptionPane”类 它应该为您提供可供选择的选项按钮 - 例如:(“ok”/“cancel”)

          【讨论】:

            猜你喜欢
            • 2015-09-03
            • 1970-01-01
            • 2015-07-14
            • 1970-01-01
            • 2014-07-25
            • 2011-10-24
            • 1970-01-01
            • 1970-01-01
            • 2019-10-25
            相关资源
            最近更新 更多