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
}
所以我有发送 按钮, CHAT TextArea 供用户输入消息和< strong> TEXT TextArea 以显示消息
但是当我输入一个消息并单击发送按钮时, TEXT 没有附加该消息,但它清除了 CHAT 即可。这意味着代码遍历这一行:
text.append(temp + newline);
中的
sendButtonActionPerformed
但没有显示任何内容。那么这里有什么不对?
答案 0 :(得分:1)
在JScrollPane scrollPane = new JScrollPane(text);
之前移动语句pack()
,这样就可以了。像这样:
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
JScrollPane scrollPane = new JScrollPane(text);