目前我正在编写一个聊天,我注意到每当我在JTextfield中写一些东西而没有将它发送到服务器时,JTextfield的当前内容 重置为""。 在我的代码中,一些字符串部分是德语的,但这不应该是我希望的一个大问题。
首先这是空JTextfield的屏幕截图: 然后我在JTextfield中写了一些东西: 之后测试文本" hg .." 2-4秒后删除而不按任何东西。
public class ClientWindow extends JFrame implements Runnable{
private static final long serialVersionUID = 7413464033743652908L;
private JPanel contentPane;
private JMenuBar bar;
private JMenu datei;
private JMenu option;
private JMenu features;
private JMenuItem verlaufSpeichern;
private JMenuItem w;
private JMenuItem ww;
private JTextArea history;
private JTextField text;
private JButton send;
private DefaultCaret caret;
private Client client;
private Thread run, listen;
public ClientWindow(String name, String address, int port){
client = new Client(name,address,port);
createWindow();
createPanel();
createLookAndFeel();
createJMenu();
createInhalt();
console("Versuche zu Verbinden.(Username: "+ name +", IP: "+ address +", Port: "+ port +")");
boolean connect = client.openConnection(address);
if(!connect){
console("Verbindung fehlgeschlagen.");
}
String connection = "/c/" + name;
client.send(connection.getBytes());
run = new Thread(this,"Running");
run.start();
}
public void run(){
listen();
}
public void listen(){
listen = new Thread("Listen") {
public void run(){
while(true){
String message = client.receive();
if(message.startsWith("/c/")){
client.setID(Integer.parseInt(message.split("/c/|/e/")[1]));
console("Erfolgreich verbunden.");
} else if(message.startsWith("/m/")){
System.out.println(message);
String text = message.substring(3);
text = text.split("/e/")[0];
console(text);
} else if(message.startsWith("/i/")){
String text = "/i/" + client.getID() + "/e/";
send(text, false);
}
}
}
};
listen.start();
}
public void createLookAndFeel(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public void createJMenu(){
bar = new JMenuBar();
datei = new JMenu(" Datei ");
option = new JMenu(" Options ");
features = new JMenu(" Features ");
verlaufSpeichern = new JMenuItem(" Verlauf speichern ");
w = new JMenuItem(" Coming Soon ");
ww = new JMenuItem(" Coming Soon ");
datei.add(verlaufSpeichern);
option.add(w);
features.add(ww);
bar.add(datei);
bar.add(option);
bar.add(features);
setJMenuBar(bar);
}
public void createWindow(){
setResizable(false);
setTitle("Chat - "+ client.getName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,550);
setLocationRelativeTo(null);
setVisible(true);
}
public void createPanel(){
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
public void createInhalt(){
history = new JTextArea();
JScrollPane scrollHistory = new JScrollPane(history);
scrollHistory.setBounds(10,10,775,450);
history.setEditable(false);
caret = (DefaultCaret) history.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
contentPane.add(scrollHistory);
text = new JTextField();
text.setBounds(10, 465, 700, 30);
text.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
send(text.getText(),true);
}
}
});
contentPane.add(text);
send = new JButton("Senden");
send.setBounds(715, 465, 70, 30);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
send(text.getText(),true);
}
});
contentPane.add(send);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
String disconnect = "/d/"+ client.getID()+"/e/";
send(disconnect,false);
client.close();
}
});
text.requestFocusInWindow();
}
public void send(String message, boolean textB){
if(message.equals("")) return;
if(textB){
message = client.getName() + ": " + message;
message = "/m/"+message;
}
client.send(message.getBytes());
text.setText("");
text.requestFocusInWindow();
}
public void console(String message){
history.append(message + "\n\r");
history.setCaretPosition(history.getDocument().getLength());
}
}
答案 0 :(得分:2)
在方法发送中,您在此行中将文本设置为“”:
text.setText("");
这是我提到的方法:
public void send(String message, boolean textB){
if(message.equals("")) return;
if(textB){
message = client.getName() + ": " + message;
message = "/m/"+message;
}
client.send(message.getBytes());
text.setText("");
text.requestFocusInWindow();
}`
我想如果删除该行,文本将不会被重置,它将解决您的问题。