gpt4 book ai didi

Java : JTextArea can not append()

转载 作者:行者123 更新时间:2023-11-30 07:52:58 25 4
gpt4 key购买 nike

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

}

所以我有一个SEND按钮,一个CHATTextArea供用户输入消息,还有一个< strong>TEXT TextArea 显示消息

但是当我输入消息并单击发送按钮时,文本并没有附加该新消息,而是清除了CHAT中的消息。这意味着代码将遍历以下行:

text.append(temp + newline);

sendButtonActionPerformed

但什么也没显示。那么这里出了什么问题?

最佳答案

将语句JScrollPanescrollPane = new JScrollPane(text);移到pack()之前就可以了。像这样:

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

关于Java : JTextArea can not append(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33095217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com