编辑1:我正在构建一个聊天应用程序,但我在保存和显示用户的昵称方面遇到了问题。 这里发生了什么:当发送消息时,昵称没有被显示,而是出现的是" null",好像保存昵称的变量是空的。
我试图将文本字段内容保存到字符串变量中,但它无效。
查找
public class NickRequest extends javax.swing.JFrame {
public String usernick;
MainScreen mainscreen;
public NickRequest() {
initComponents();
}
private void initComponents() {
lblEscolhaApelido = new javax.swing.JLabel();
txtUserNick = new javax.swing.JTextField();
lblDigite = new javax.swing.JLabel();
btnOk = new javax.swing.JButton();
btnCancelar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblEscolhaApelido.setText("Escolha seu apelido");
txtUserNick.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtUserNickActionPerformed(evt);
}
});
lblDigite.setText("Digite:");
btnOk.setText("Ok");
btnOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOkActionPerformed(evt);
}
});
btnCancelar.setText("Cancelar");
btnCancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelarActionPerformed(evt);
}
});
pack();
}
private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
this.usernick = txtUserNick.getText();
mainscreen = new MainScreen();
mainscreen.setVisible(true);
}
private void txtUserNickActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NickRequest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancelar;
private javax.swing.JButton btnOk;
private javax.swing.JLabel lblDigite;
private javax.swing.JLabel lblEscolhaApelido;
private javax.swing.JTextField txtUserNick;
// End of variables declaration
}
我已经阅读了有关类似问题的其他帖子,但它对我没有帮助,我无法看清我做错了什么。
历史悠久:
我从Netbeans生成的内容改变了这一点:
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
this.usernick = txtUserNick.getText();
mainscreen = new MainScreen();
mainscreen.setVisible(true);
}
编辑2: 所以问题不是保存变量,而是显示其内容。 当消息发送给另一个用户时,发射器的昵称显示为" null"。
以下是我使用的代码:
package chatapp.gui;
import chatapp.networking.MessageListener;
import chatapp.networking.MessageTransmitter;
import chatapp.networking.WritableGUI;
import java.time.LocalDateTime;
public class MainScreen extends javax.swing.JFrame implements WritableGUI{
NickRequest nickrequest = new NickRequest();
public MainScreen() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jInternalFrame1 = new javax.swing.JInternalFrame();
txtIpTextField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtTargetPort = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
chat = new javax.swing.JTextArea();
txtMensagem = new javax.swing.JTextField();
btnEnviar = new javax.swing.JButton();
listenButton = new javax.swing.JButton();
receivePort = new javax.swing.JTextField();
jInternalFrame1.setVisible(true);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat App - APS 5º Semestre - UNIP/Bauru");
jLabel1.setText("IP:");
jLabel2.setText("Porta:");
chat.setColumns(20);
chat.setRows(5);
jScrollPane1.setViewportView(chat);
btnEnviar.setText("Enviar");
btnEnviar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEnviarActionPerformed(evt);
}
});
listenButton.setText("Ouvir");
listenButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
listenButtonActionPerformed(evt);
}
});
MessageListener listener;
private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {
listener = new MessageListener(this, Integer.parseInt(receivePort.getText())); //Inicializa a MessageListener thread
listener.start(); //Por ser uma thread, campo inicia a thread
}
private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {
MessageTransmitter transmitter = new MessageTransmitter (txtMensagem.getText(), txtIpTextField.getText(), Integer.parseInt(txtTargetPort.getText()));
transmitter.start();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnEnviar;
private javax.swing.JTextArea chat;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton listenButton;
private javax.swing.JTextField receivePort;
private javax.swing.JTextField txtIpTextField;
private javax.swing.JTextField txtMensagem;
private javax.swing.JTextField txtTargetPort;
// End of variables declaration
@Override
public void write(String s) {
chat.append(" " + LocalDateTime.now().getHour() + "h" + LocalDateTime.now().getMinute() + "min - " + nickrequest.usernick + ": " + s + System.lineSeparator()); //Toda vez que o método write for chamado e uma string for passada como parâmetro, um "s" e uma nova linha serão emendados a essa string
}
}