出于一个原因,getText()返回null。例如:
somebody:hi!
显示为
null:hi!
我有一个按钮,你必须按才能更改名称,但它仍然会返回null,好像没有在JTextField中写入任何内容。如果我没有'按下按钮,然后使用:
username=getText();
它显示为:
:hi!
这是我的代码(是的,我同意布局很糟糕,但目前我正在努力让它成功):
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TextClient extends JFrame{
public JPanel mypanel=new JPanel(new GridBagLayout());
public static JButton send=new JButton("Send");
public static JButton changename=new JButton("Change name");
public static JTextField textf=new JTextField(10);
public static JTextField textname=new JTextField(10);
public static JLabel username=new JLabel("Name:");
public static JTextArea texta=new JTextArea(20,20);
public static JScrollPane jsp=new JScrollPane(texta);
public String user;
public TextClient() throws IOException
{
add(mypanel);
GridBagConstraints c1=new GridBagConstraints();
c1.anchor=GridBagConstraints.SOUTH;
c1.gridx=3;
c1.gridy=1;
mypanel.add(textf,c1);
GridBagConstraints c2=new GridBagConstraints();
c2.gridx=2;
c2.gridy=2;
mypanel.add(jsp,c2);
GridBagConstraints c3=new GridBagConstraints();
c3.gridx=2;
c3.gridy=0;
mypanel.add(send,c3);
GridBagConstraints c4=new GridBagConstraints();
c4.gridx=0;
c4.gridy=0;
mypanel.add(username,c4);
GridBagConstraints c5=new GridBagConstraints();
c5.gridx=1;
c5.gridy=0;
mypanel.add(textname,c5);
GridBagConstraints c6=new GridBagConstraints();
c6.gridx=2;
c6.gridy=1;
mypanel.add(changename,c6);
setVisible(true);
setSize(500,500);
setLocationRelativeTo(null);
setResizable(false);
Socket socket=new Socket("localhost",9000);
changename.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ev)
{
user=textname.getText();
}
});
ClientPeer cp=new ClientPeer(user,socket);
String message=textf.getText();
cp.start();
send.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ev)
{
try {
String sendit=textf.getText();
cp.sendMessage(sendit);
textf.setText("");
} catch (IOException ex) {
Logger.getLogger(TextClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public static void main(String [] args)
{
try {
new TextClient();
} catch (IOException ex) {
Logger.getLogger(TextClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:2)
混合对象和变量,并在错误的时间拨打一个电话。
下面:
ClientPeer cp=new ClientPeer(user,socket);
String message=textf.getText();
您在构造函数中而不是在ActionListener中调用getText(),这意味着您在textf JTextField甚至已经在GUI中呈现之前,并且在用户有机会填充它之前就进行了此调用。
这里:
@Override
public void actionPerformed(ActionEvent ev)
{
user=textname.getText();
}
您更改了ActionListener中用户字段的状态,但遗憾的是您在我显示的第一个代码中使用了用户可变量,再次 字段已经呈现并且在用户之前与之互动。
我建议您添加更多代码,包括使用内部 ActionListener代码的代码,以便变量实际包含相关信息。
其他建议:
以下是代码的简化版本,向您展示我的意思:
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TextClient extends JFrame {
public JPanel mypanel = new JPanel();
public JButton send = new JButton("Send");
public JButton changename = new JButton("Change name");
public JTextField textf = new JTextField(10);
public JTextField textname = new JTextField(10);
public JLabel username = new JLabel("Name:");
public String user;
public TextClient() throws IOException {
add(mypanel);
mypanel.add(textf);
mypanel.add(send);
mypanel.add(username);
mypanel.add(textname);
mypanel.add(changename);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
setLocationRelativeTo(null);
changename.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
user = textname.getText();
System.out.println("user in action listener: " + user);
}
});
// you're trying to use user here!
System.out.println("user outside of action "
+ "listener where you try to use it: " + user); // !!
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
String sendit = textf.getText();
System.out.println("sendit: " + sendit);
}
});
}
public static void main(String[] args) {
try {
new TextClient();
} catch (IOException ex) {
Logger.getLogger(TextClient.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}