Java简单聊天框

发布于 2024-08-25 07:45:26 字数 1584 浏览 9 评论 0原文

我正在尝试创建一个非常简单的聊天窗口,它只能显示一些我不时添加的文本。但是,当尝试将文本附加到窗口时,我收到以下运行时错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒岛晴空 2024-09-01 07:45:26

您有两个选择:

  1. JTextPane 存储在成员变量中,然后在 getTextPane() 内返回该变量。
  2. 修改 getTextPane 以返回 JViewPort 的视图,如下所示

    return (JTextPane) getViewport().getView();
    

请参阅Swing 教程 了解更多详细信息。

另外,正如 camickr(和教程)指出的那样,将 addJScrollPane 一起使用是不正确的。您应该将组件传递给构造函数或使用 setViewportView

顺便说一句,除非绝对必要(更喜欢组合而不是继承),否则我尽量不要对 Swing 组件进行子类化。但这与问题并不是特别相关。

You have two options:

  1. Store the JTextPane in a member variable and return that inside getTextPane().
  2. Modify getTextPane to return the JViewPort's view, like this

    return (JTextPane) getViewport().getView();
    

See the Swing tutorials for more detail.

Also, as camickr (and the tutorials) pointed out, using add with a JScrollPane is incorrect. You should be either passing the component to the constructor or using setViewportView.

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.

悲凉≈ 2024-09-01 07:45:26

不要扩展 JScrollPane。您没有向其添加任何功能。

看来基本问题是您正在尝试将文本窗格添加到滚动窗格。这不是它的工作方式。您需要将文本窗格添加到视口。执行此操作的简单方法是:

JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane( textPane );

scrollPane.setViewportView( textPane );

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:

JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane( textPane );

or

scrollPane.setViewportView( textPane );
浅黛梨妆こ 2024-09-01 07:45:26
public JTextPane getTextPane() {
    return (JTextPane) this.getComponent(0);
}

this.getComponent(0) 返回 ScrollPane 的 JViewPort,而不是您的 JTextPane。它无法被转换,所以你得到了例外。

public JTextPane getTextPane() {
    return (JTextPane) this.getComponent(0);
}

this.getComponent(0) is returning the ScrollPane's JViewPort, not your JTextPane. It can't be casted, and so you get your exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文