나는 결국 HTML 링크를 보여주고 싶은 간단한 채팅 프로그램을 만들고 있습니다. 내 문제는 바로 내가 원하는대로 사용자 이름 옆에 텍스트를 표시 할 수 없다는 것입니다.JTextPane/JEditorPane 및 이상한 텍스트 문제
사용자 이름을 굵게 표시하고 텍스트를 바로 옆에 표시하고 싶지만 굵게 표시되지 않은 텍스트가 가운데에 표시됩니다.
사용자 이름을 굵게 표시하지 않으면 정상적으로 작동합니다. 상위 2 가지는 이름이 굵게 표시 될 때, 중간은 이름이 굵게 표시되지 않을 때, 밑줄은 중간 2처럼 표시되도록 하이퍼 링크를 표시하지만 이름은 굵게 표시됩니다. 여기
내가 뭘 잘못 코드인가? JTextPane를 JEditorPane로 바꾸려고 시도했는데 같은 일이 발생합니다.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) {
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);
}
}
잘 작동하고 짧은 데모 프로그램을 게시하기 위해 1+, 문제를 잘 보여주는 프로그램. –
저는 JTextPane 전문가는 아니지만 'pane.setContentType ("text/html");'행을 주석 처리하면 문제가 사라지는 것에 유의합니다. –
그래, 문제가 해결되었다는 사실을 알고 있습니다. 하이퍼 링크를 표시 할 수 있어야하기 때문에 text/html을 사용하고 있습니다. 텍스트/html로만 작동하는 것 같습니다. – systemoutprintln