我正在创建一个简单的聊天程序,我希望最终显示 html 链接。我现在的问题是我无法让文本按照我的意愿出现在用户名旁边。
我希望用户名是粗体,并且文本显示在它旁边,但由于某种原因,非粗体文本显示居中。
如果我不加粗用户名,它可以正常工作。前两个是我将名称加粗时的显示方式,中间是名称不加粗时的显示方式,底部显示一个超链接,我希望它看起来像中间两个,但名称加粗。
这是代码,我做错了什么?请注意,我尝试用 JEditorPane 替换 JTextPane 并且发生了同样的事情。
package com.test;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
public class JTextPaneTest extends JPanel {
JTextPane pane;
public JTextPaneTest() {
this.setLayout(new BorderLayout());
pane = new JTextPane();
pane.setEditable(false);
pane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(pane);
this.add(scrollPane, BorderLayout.CENTER);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == EventType.ACTIVATED) {
System.out.println(e.getDescription());
}
}
});
}
public void chatWithBold(String user, String text) {
SimpleAttributeSet bold = new SimpleAttributeSet();
StyleConstants.setBold(bold, true);
SimpleAttributeSet normal = new SimpleAttributeSet();
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
user + ": ", bold);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
text + "\n", normal);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void chatNoBold(String user, String text) {
SimpleAttributeSet bold = new SimpleAttributeSet();
StyleConstants.setBold(bold, true);
SimpleAttributeSet normal = new SimpleAttributeSet();
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
user + ": ", normal);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
text + "\n", normal);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void submitALinkWithBold(String user, String link) {
SimpleAttributeSet bold = new SimpleAttributeSet();
StyleConstants.setBold(bold, true);
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
user + ": ", bold);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTML.Attribute.HREF, link);
SimpleAttributeSet htmlLink = new SimpleAttributeSet();
htmlLink.addAttribute(HTML.Tag.A, attrs);
StyleConstants.setUnderline(htmlLink, true);
StyleConstants.setForeground(htmlLink, Color.BLUE);
try {
pane.getDocument().insertString(pane.getDocument().getLength(),
link + "\n", htmlLink);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame frame = new JFrame();
JTextPaneTest chat = new JTextPaneTest();
frame.add(chat);
frame.setDefaultCloseOperation
(WindowConstants.DISPOSE_ON_CLOSE);
chat.chatWithBold("User1", "Hi everyone");
chat.chatWithBold("User2", "Hey.. Hows it going");
chat.chatNoBold("User1", "Hi everyone");
chat.chatNoBold("User2", "Hey.. Hows it going");
chat.submitALinkWithBold("User1", "http://www.stackoverflow.com");
frame.setSize(400, 400);
frame.setVisible(true);
}
});
}
}