Java简单聊天框
我正在尝试创建一个非常简单的聊天窗口,它只能显示一些我不时添加的文本。但是,当尝试将文本附加到窗口时,我收到以下运行时错误:
java.lang.ClassCastException: javax.swing.JViewport cannot be cast to javax.swing.JTextPane
at ChatBox.getTextPane(ChatBox.java:41)
at ChatBox.getDocument(ChatBox.java:45)
at ChatBox.addMessage(ChatBox.java:50)
at ImageTest2.main(ImageTest2.java:160)
这是处理基本操作的类:
public class ChatBox extends JScrollPane {
private Style style;
public ChatBox() {
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
this.add(textPane);
}
public JTextPane getTextPane() {
return (JTextPane) this.getComponent(0);
}
public StyledDocument getDocument() {
return (StyledDocument) getTextPane().getStyledDocument();
}
public void addMessage(String speaker, String message) {
String combinedMessage = speaker + ": " + message;
StyledDocument document = getDocument();
try {
document.insertString(document.getLength(), combinedMessage, style);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
}
}
如果有更简单的方法来执行此操作,请务必让我知道。我只需要文本为单一字体类型,并且用户无法编辑。除此之外,我只需要能够动态添加文本。
I am trying to create a very simple chat window that simply has the ability to display some text, which I add to from time to time. However I get the following run time error when attempting to append text to the window:
java.lang.ClassCastException: javax.swing.JViewport cannot be cast to javax.swing.JTextPane
at ChatBox.getTextPane(ChatBox.java:41)
at ChatBox.getDocument(ChatBox.java:45)
at ChatBox.addMessage(ChatBox.java:50)
at ImageTest2.main(ImageTest2.java:160)
Here is the class to handle the basic operations:
public class ChatBox extends JScrollPane {
private Style style;
public ChatBox() {
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
this.add(textPane);
}
public JTextPane getTextPane() {
return (JTextPane) this.getComponent(0);
}
public StyledDocument getDocument() {
return (StyledDocument) getTextPane().getStyledDocument();
}
public void addMessage(String speaker, String message) {
String combinedMessage = speaker + ": " + message;
StyledDocument document = getDocument();
try {
document.insertString(document.getLength(), combinedMessage, style);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
}
}
If there is a simpler way to do this, by all means let me know. I only need the text to be of a single font type, and uneditable by the user. Aside from that, I just need to be able to append text on the fly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个选择:
JTextPane
存储在成员变量中,然后在getTextPane()
内返回该变量。修改
getTextPane
以返回JViewPort
的视图,如下所示请参阅Swing 教程 了解更多详细信息。
另外,正如 camickr(和教程)指出的那样,将
add
与JScrollPane
一起使用是不正确的。您应该将组件传递给构造函数或使用setViewportView
。顺便说一句,除非绝对必要(更喜欢组合而不是继承),否则我尽量不要对 Swing 组件进行子类化。但这与问题并不是特别相关。
You have two options:
JTextPane
in a member variable and return that insidegetTextPane()
.Modify
getTextPane
to return theJViewPort
's view, like thisSee the Swing tutorials for more detail.
Also, as camickr (and the tutorials) pointed out, using
add
with aJScrollPane
is incorrect. You should be either passing the component to the constructor or usingsetViewportView
.As a side note, I try not to subclass Swing components unless it's absolutely necessary (preferring composition over inheritance). But that's not particularly relevant to the question.
不要扩展 JScrollPane。您没有向其添加任何功能。
看来基本问题是您正在尝试将文本窗格添加到滚动窗格。这不是它的工作方式。您需要将文本窗格添加到视口。执行此操作的简单方法是:
或
Don't extend a JScrollPane. You are NOT adding any functionality to it.
It looks like the basic problem is that your are trying to add the text pane to the scrollpane. This is not the way it works. You need to add the text pane to the viewport. The easy way to do this is:
or
this.getComponent(0)
返回 ScrollPane 的JViewPort
,而不是您的JTextPane
。它无法被转换,所以你得到了例外。this.getComponent(0)
is returning the ScrollPane'sJViewPort
, not yourJTextPane
. It can't be casted, and so you get your exception.