8

我有一个 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 中。例如,我希望它打印一条消息,上面写着“文件已上传到服务器。接受*Decline*”,如果用户单击接受或拒绝字符串,则它会执行相应的功能。关于如何有效实现这一点的任何想法?

4

5 回答 5

7

我最终用一个 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();
        }
    }
于 2013-04-23T16:24:49.820 回答
3

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

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

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

于 2013-04-21T17:39:31.890 回答
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);
于 2013-07-08T18:45:31.833 回答
2

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

于 2013-04-21T14:01:12.890 回答
0

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

于 2013-04-21T15:33:52.050 回答