JFileChooser 在 Java、NetBeans 中不起作用

JFileChooser does not work in Java, NetBeans

我会总结一下,对不起我的英语。

我有一个客户端和一个服务器线程,当触摸一个按钮发送文件时正确显示带有 showOpenDialog 的 JFileChooser,当收到文件时想在服务器线程中创建第二个 JFileChooser 时,只有一个空白屏幕出现出来了。

我已经在 class、函数、新 JFrame 和同一函数中创建了代码。

据我所知,问题出现在创建第二个 JFileChooser 时,因为如果在第一个 JFileChooser 中我通过 showSaveDialog 更改它,它显示得很好。

代码:

class GuardarArchivo extends javax.swing.JFrame {

  JFrame jf = new JFrame();
  jf.setAlwaysOnTop(true);

  JFileChooser elegirRuta = new JFileChooser();

  elegirRuta.setDialogTitle("Selecciona donde guardar el archivo: ");
  int returnVal = elegirRuta.showSaveDialog(jf);

  System.out.print(returnVal);
  ruta = elegirRuta.getSelectedFile().getAbsolutePath();

}

图片:

您的问题可能在这些评论中有所体现:

I have a client and a server thread, when touching a button send file is correctly displayed the JFileChooser with showOpenDialog, when want to create a second JFileChooser in the server thread when a file is received, only a blank screen comes out.

您可能遇到线程问题。您不应在除 Swing 事件分派线程(EDT)之外的任何线程中打开 JFileChooser。文件传输代码可能会阻止 EDT,阻止它执行其必要的功能,包括绘制 GUI 的功能,因此全白对话框。由于您没有发布有效的 MCVE I cannot give you an exact solution, other than to recommend that you take care to do all long running code in threads off of the EDT, and all Swing code on the EDT. For more on Swing threading issues please read: Lesson: Concurrency in Swing.

为了获得更好的帮助,请考虑创建并发布 Minimal, Complete, and Verifiable Example Program。我们不想看到你的整个程序,而是你应该将你的代码压缩成仍然可以编译的最小部分,没有与你的问题无关的额外代码,但仍然可以证明你的问题。您可以通过简单地尝试隔离和公开错误来很好地自己解决问题。