要更新问题,我已将产品和客户类附加到服务器和客户端类
之下 如果我不清楚的话,我会提前在这里道歉。我正在尝试在String
中显示ArrayList
中包含的JTextField
值。 String
值会显示,但null
值也会显示,我想显示String
值。这是String
中显示的示例JTextField
值
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
在我的程序中,我有客户端用户界面从String
Server
请求Object
值,然后在JTextField
中显示它们。这是客户端界面代码:
private void jButtonDisplaycNamesActionPerformed(java.awt.event.ActionEvent evt) {
{
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.NAME);
out.writeObject(request);
out.flush();
List<Customer> customerNames = (List<Customer>) in.readObject();
jTextAreaOutput.setText("");
for (Customer customerName : customerNames) {
jTextAreaOutput.append(customerNames.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
// TODO add your handling code here:
}
private void jButtonDisplaycIDActionPerformed(java.awt.event.ActionEvent evt) {
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.ID);
out.writeObject(request);
out.flush();
List<Customer> customerIDs = (List<Customer>) in.readObject();
jTextAreaOutput.setText("");
for (Customer customerID : customerIDs) {
jTextAreaOutput.append(customerIDs.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
private void jButtonDisplaypIDActionPerformed(java.awt.event.ActionEvent evt) {
{
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.PRODUCTID);
out.writeObject(request);
out.flush();
List<Product> productIDs = (List<Product>) in.readObject();
jTextAreaOutput.setText("");
for (Product procuctID : productIDs) {
jTextAreaOutput.append(productIDs.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
}
这是服务器对象代码
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket server = new ServerSocket(67);
while (true) {
Socket socket = server.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
DataRequest request = (DataRequest) in.readObject();
switch (request.getDataType()) {
case DataRequest.NAME:
out.writeObject(getCustomerName());
out.flush();
break;
case DataRequest.ID:
out.writeObject(getCustomerID());
out.flush();
break;
case DataRequest.PASSWORD:
out.writeObject(getCustomerPassword());
out.flush();
break;
case DataRequest.DESCRIPTION:
out.writeObject(getProductDescription());
out.flush();
break;
case DataRequest.PRODUCTID:
out.writeObject(getProductDescription());
out.flush();
break;
case DataRequest.PRICE:
out.writeObject(getProductDescription());
out.flush();
break;
}
}
}
private static List<Customer> getCustomerName() {
List<Customer> names = new ArrayList<Customer>();
in.readObject;
Customer name = new Customer();
name.setcFirstName("Damon");
name.setcLastName("Tholson");
names.add(name);
name = new Customer();
name.setcFirstName("Sylvester");
name.setcLastName("Stallone");
names.add(name);
name = new Customer();
name.setcFirstName("Mister");
name.setcLastName("T");
names.add(name);
return names;
}
公共类Customer实现Serializable {
private String cID;
private String cFirstName;
private String cLastName;
private String cPassword;
private int dataType;
/**
* @return the cID
*/
public String getcID() {
return cID;
}
/**
* @param cID the cID to set
*/
public void setcID(String cID) {
this.cID = cID;
}
/**
* @return the cFirstName
*/
public String getcFirstName() {
return cFirstName;
}
/**
* @param cFirstName the cFirstName to set
*/
public void setcFirstName(String cFirstName) {
this.cFirstName = cFirstName;
}
/**
* @return the cLastName
*/
public String getcLastName() {
return cLastName;
}
/**
* @param cLastName the cLastName to set
*/
public void setcLastName(String cLastName) {
this.cLastName = cLastName;
}
/**
* @return the cPassword
*/
public String getcPassword() {
return cPassword;
}
/**
* @param cPassword the cPassword to set
*/
public void setcPassword(String cPassword) {
this.cPassword = cPassword;
}
/**
* @return the dataType
*/
public int getDataType() {
return dataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
this.dataType = dataType;
}
@Override
public String toString() {
return cID + cFirstName + cLastName + cPassword + dataType ;
}
public class Product实现Serializable {
private String pID;
private String pDescription;
private String pPrice;
private int dataType;
/**
* @return the pID
*/
public String getpID() {
return pID;
}
/**
* @param pID the pID to set
*/
public void setpID(String pID) {
this.pID = pID;
}
/**
* @return the pName
*/
/**
* @return the pDescription
*/
public String getpDescription() {
return pDescription;
}
/**
* @param pDescription the pDescription to set
*/
public void setpDescription(String pDescription) {
this.pDescription = pDescription;
}
/**
* @return the pPrice
*/
public String getpPrice() {
return pPrice;
}
/**
* @param pPrice the pPrice to set
*/
public void setpPrice(String pPrice) {
this.pPrice = pPrice;
}
public int getDataType() {
return dataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
this.dataType = dataType;
}
@Override
public String toString() {
return pID + pDescription + pPrice;
}
/**
* @return the dataType
*/
}
答案 0 :(得分:0)
问题出在Client类中,它调用Customer类中toString方法的输出。我在toString方法中有多个返回但是我只调用了一个返回,所以我收到了我调用的变量和我没有调用的变量的空值。所以我的toString方法返回以下内容:
@覆盖 public String toString() { return cFirstName + cID + cLastName + cPassword; }
但是我的Client类只调用该方法中的一个变量
for(Customer customerName:customerNames) { jTextAreaOutput.append(customerName.toString());
}
所以我改为调用特定变量
for(Customer customerName:customerNames) { jTextAreaOutput.append(customerName.getcFirstName());
}