jTextField.setText()在Jframe中不起作用



我用jDialog创建了一个简单的jFrame,在这个jDialog中,我有文本字段来获取ID,他们用这个ID使用Web服务来搜索信息,并在jFrame中显示信息,但是,我在jFrame上使用的所有文本字段都不是setText(),为什么会发生这种情况,我该如何解决?

以下是我使用Web服务并显示信息的代码:

public void Buscar () throws Exception {

InterfaceConsu2 IFC2 = new InterfaceConsu2();
ConsumirWS2 http = new ConsumirWS2();
Gson g = new GsonBuilder().setDateFormat("dd-MM-yyyy").create();
Cliente2 u = new Cliente2();
Type ClienteType = new TypeToken<Cliente2>() {
}.getType();
u.setCad_pes_cpf(DialogCPF.jFormattedTextField1.getText());
String url = "http://localhost:8080/clienteWebService/webresources/CadastroCliente/Cliente/get/"+u.getCad_pes_cpf();

String json = http.sendGet(url, "GET");
u = g.fromJson(json, ClienteType);
System.out.println(json + ("n"));
System.out.println(u.getcad_pes_nome() +("n") + u.getCad_pes_apelido() +("n") + u.getCad_pes_cpf() +("n") + u.getCad_pes_data()+("n"));
String Date1 = new SimpleDateFormat("dd-MM-yyyy").format(u.getCad_pes_data());
System.out.println(Date1);

IFC2.jTextFieldNOME.setText(u.getcad_pes_nome());
IFC2.jTextFieldAPELIDO.setText(u.getCad_pes_apelido());
IFC2.jFormattedTextFieldCPF.setText(u.getCad_pes_cpf());
IFC2.jFormattedTextFieldDATA.setText(Date1);
IFC2.jTextFieldID.setText(u.getcad_pes_id());

}

使用System.out.println(u.getcad_pes_nome() +("n") + u.getCad_pes_apelido() +("n") + u.getCad_pes_cpf() +("n") + u.getCad_pes_data()+("n"));,我接收所有信息并显示在控制台上,但是,在我的jFrame中没有

您的问题可能是由于您设置了错误实例的状态,这里是一个新的InterfaceConsu2对象,它可能与显示的对象完全无关(可能是JFrame(,因为您似乎在方法中创建了一个新实例。

解决方案建议:

  • 不创建新的InterfaceConsu2实例
  • 不要尝试使用静态字段解决此问题
  • 相反,给InterfaceConsu2类setter方法,允许外部类更新其状态,而不必直接接触字段,应该是私有的字段
  • 将可视化InterfaceConsu2的引用传递到需要的位置
  • 更好的是,了解并使用M-V-C(模型视图控制器(设计模式,并在需要时更新模型。当模型的侦听器通知视图模型状态更改时,视图应该更新自己
  • 顺便说一句,您将希望学习和使用Java命名约定。变量名都应该以小写字母开头,而类名应该以大写字母开头。学习并遵循这一点将使我们更好地理解您的代码,并使您更好地理解他人的代码
  • 您在发布的方法中似乎有一些长时间运行的代码。如果是这样,请在后台线程中运行此代码,例如在SwingWorker中运行,以免冻结GUI

最新更新