首页 > 解决方案 > Java Chatbox appending additional white space

问题描述

I have a simple Chatbox that has the following code:

public class Chatbox extends JFrame
{
    JPanel mainPanel;
    JTextArea inputField;
    JTextArea chatTextArea;
    JLabel chatLabel;
    boolean typing;
    Timer t;

    public Chatbox()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        // Set frame properties
        setTitle("Plain Text Editor - <ID>");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Create a JPanel and set layout
        mainPanel=new JPanel();
        mainPanel.setLayout(new GridLayout(0,1));


        // Create JTextField, add it.
        inputField=new JTextArea();

        //inputField.setWrapStyleWord(true);
        inputField.setLineWrap(true);

        JScrollPane sp = new JScrollPane(inputField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        mainPanel.add(sp);

        // Add panel to the south,
        add(mainPanel,BorderLayout.SOUTH);


        // Add a KeyListener
        inputField.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent ke)
            {    
                // If he presses enter, add text to chat textarea
                if(ke.getKeyCode()==KeyEvent.VK_ENTER) showLabel(inputField.getText());
            }


        });

        // Create a textarea
        chatTextArea=new JTextArea();

        // Make it non-editable
        chatTextArea.setEditable(false);

        // Set some margin, for the text
        chatTextArea.setMargin(new Insets(7,7,7,7));

        chatTextArea.setLineWrap(true);
        chatTextArea.setWrapStyleWord(true);


        // Set a scrollpane
        JScrollPane js=new JScrollPane(chatTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);  
        add(js);

        addWindowListener(new WindowAdapter(){
            public void windowOpened(WindowEvent we)
            {
                // Get the focus when window is opened
                inputField.requestFocus();
            }
        });

        setSize(400,400);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void showLabel(String text)
    {
        // If text is empty return
        if(text.trim().isEmpty()) return;

        // Otherwise, append text with a new line
        chatTextArea.append(text+"\n");

        // Set textfield and label text to empty string
        inputField.setText("");
    }
}

However, there is an additional space being appended when I press enter into the TextArea. How can I make it so that messages are being appended separately without an additional white white space?

I'm also not sure if the JScrollPane is messing up the spacing as this issue was not happening before I implemented the JScrollPane.

标签: javatextarea

解决方案


chatTextArea.append(text+"\n");每次添加新文本时都会添加一个新行。

相反,您可以考虑在文本前加上一个新行(当文本已经存在于聊天窗口中时),例如...

private void showLabel(String text) {
    // If text is empty return
    if (text.trim().isEmpty()) {
        return;
    }

    if (!chatTextArea.getText().isEmpty()) {
        text = "\n" + text;
    }

    // Otherwise, append text with a new line
    chatTextArea.append(text);

    // Set textfield and label text to empty string
    inputField.setText("");
}

您的输入也将增加一条新线JTextArea(这让我花了一些时间才意识到,因为我期待 a JTextField

发生这种情况是因为KeyEvent.VK_ENTER最有可能在 your 之前由JTextArea(及其底层Document)处理KeyListener,这意味着新行现在实际上是文本的一部分。

您可能会考虑在将文本发送到showLabel方法之前对其进行修剪,例如...

inputField.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent ke) {
        // If he presses enter, add text to chat textarea
        if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
            showLabel(inputField.getText().trim());
        }
    }
});

但是,更好的解决方案是尝试String手动trim删除末尾的空格,以及删除前导和尾随空格,但我会让你弄清楚


推荐阅读