简体   繁体   English

Java的:JTextArea不能append()

[英]Java : JTextArea can not append()

package my.test;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;


public class ChatP2P extends javax.swing.JFrame {
    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    String name = LoginInterface.id;
    private final static String newline = "\n";
    String message = null;

    /**
     * Creates new form ChatP2P
    */
    public ChatP2P(Socket socket) throws IOException, ClassNotFoundException {
        initComponents();
        JScrollPane scrollPane = new JScrollPane(text); 
        text.setLineWrap(true);
        chat.setLineWrap(true);
        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
        new Thread(new receiveThread(message, in, text)).start();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuItem1 = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        text = new javax.swing.JTextArea();
        nameField = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        chat = new javax.swing.JTextArea();
        sendButton = new javax.swing.JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        text.setEditable(false);
        text.setColumns(20);
        text.setRows(5);
        jScrollPane1.setViewportView(text);

        nameField.setEditable(false);

        chat.setColumns(20);
        chat.setRows(5);
        jScrollPane3.setViewportView(chat);

        sendButton.setText("SEND");
        sendButton.addActionListener(new java.awt.event.ActionListener()  {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(nameField)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(1, 1, 1)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
    );

        pack();
    }// </editor-fold>                        

private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (!chat.getText().equals("")){
        try {
            String temp = name + " : " + chat.getText();
            out.writeObject(temp);
            text.append(temp + newline);
            chat.setText("");
        } catch (IOException ex) {
            Logger.getLogger(ChatP2P.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}                                          

/**
 * @param args the command line arguments
 */


// Variables declaration - do not modify                     
private javax.swing.JTextArea chat;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
public javax.swing.JTextField nameField;
private javax.swing.JButton sendButton;
public javax.swing.JTextArea text;
// End of variables declaration                   

} }

So I have a SEND button , a CHAT TextArea for user to input the message and a TEXT TextArea to display the message 所以,我有一个SEND 聊天 文本区域用于用户为输入该消息和文本 的TextArea显示该消息

but when I typed a messsage and clicked on the SEND button,the TEXT didn't append that new message,but it cleared the message in CHAT . 但是当我键入一个消息并单击“ 发送”按钮时, TEXT未附加该新消息,但它清除了CHAT中的消息。 That means the code walks through the line : 这意味着代码行如下:

text.append(temp + newline);

in the 在里面

sendButtonActionPerformed

but nothing is displayed. 但没有显示任何内容。 So what's wrong here ? 那这怎么了?

Move statement JScrollPane scrollPane = new JScrollPane(text); 移动语句JScrollPane scrollPane = new JScrollPane(text); before pack() and it will be ok. pack()之前,就可以了。 Like this: 像这样:

jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
JScrollPane scrollPane = new JScrollPane(text); 

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

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