简体   繁体   English

随着聊天的进行,如何使聊天程序向下滚动?

[英]How do I make my chat program scroll down as the chat moves along?

I'm making a simple chat GUI. 我正在制作一个简单的聊天GUI。 I've come into a problem where my program isn't scrolling down as the chat moves along. 我遇到了一个问题,即随着聊天的进行,我的程序没有向下滚动。 I'm also unsure about how to add a scroll bar/pane to the program, without messing everything up by putting my main text area into a panel and destroying the look of the interface. 我也不确定如何在程序中添加滚动条/窗格,而不会通过将主要文本区域放入面板并破坏界面外观来弄乱一切。 How do I adjust the main chat box, without screwing it up and making it look ugly by putting the chatBox in a JPanel. 我如何通过将chatBox放在JPanel中来调整主聊天框,而不用拧紧它并使它看起来丑陋。 I'll post all of my code below. 我将在下面发布所有代码。

MainGUI class: MainGUI类:

package coltGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

public class MainGUI implements ActionListener {

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        MainGUI gui = new MainGUI();
        gui.display();
    }

    JButton sendMessage;
    JTextField messageBox;
    JTextArea chatBox;

    public void display() {

        JFrame frame = new JFrame("Colt Chat");
        JPanel southPanel = new JPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, southPanel);
        southPanel.setBackground(Color.BLUE);
        southPanel.setLayout(new GridBagLayout());

        messageBox = new JTextField(30);
        sendMessage = new JButton("Send Message");
        chatBox = new JTextArea();
        chatBox.setEditable(false);
        frame.getContentPane().add(BorderLayout.CENTER, chatBox);

        chatBox.setLineWrap(true);

        GridBagConstraints left = new GridBagConstraints();
        left.anchor = GridBagConstraints.WEST;
        GridBagConstraints right = new GridBagConstraints();
        right.anchor = GridBagConstraints.EAST;
        right.weightx = 2.0;

        southPanel.add(messageBox, left);
        southPanel.add(sendMessage, right);

        sendMessage.addActionListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(470, 300);
    }

    public void actionPerformed(ActionEvent event) {
        if (messageBox.getText().length() < 1) {
            // do nothing 
        } else {
            chatBox.append(messageBox.getText() + "\n");
            messageBox.setText("");
        }
    }
}

Just add the text area to a JScrollPane and then add the scrollpane to the frame. 只需将文本区域添加到JScrollPane,然后将滚动窗格添加到框架。 There is no need for a panel. 不需要面板。

//frame.getContentPane().add(BorderLayout.CENTER, chatBox);
frame.add(new JScrollPane(chatBox), BorderLayout.CENTER);

Note that constraints should be specified as the second parameter of the add(...) method, not the first. 请注意,约束应指定为add(...)方法的第二个参数,而不是第一个。

Also, since JDK5, you don't need to use getContentPane(), the frame.add(..) method will do this for you. 另外,由于JDK5,您不需要使用getContentPane(),frame.add(..)方法将为您完成此操作。

If you want automatic scrolling you can check out Text Area Scrolling . 如果要自动滚动,可以签出“ 文本区域滚动”

First you need to wrap chatBox in a JScrollPane like this: 首先,您需要将chatBox包装在JScrollPane如下所示:

frame.add(new JScrollPane(chatBox), BorderLayout.CENTER);

Second after appending the message to chatBox you need to force it to scroll to the end which can be done with the following: 将消息附加到chatBox之后, chatBox需要强制其滚动到末尾,这可以通过以下操作完成:

chatBox.setCaretPosition(chatBox.getDocument().getLength());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM