JFileChooser中不停止运行
问题描述:
我一直在尝试使用JFileChooser的,但我有问题,该程序不会停止运行,这里是我的代码:JFileChooser中不停止运行
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class copiarArcivos {
public static void main(String[] args) {
JFileChooser();
}
public static void JFileChooser(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
}
我应该简单地把休息的如果结束?
答
请勿创建空的JFrame。您可以使用null:
//int result = fileChooser.showOpenDialog(new JFrame());
int result = fileChooser.showOpenDialog(null);
答
您必须在main方法中更改JFileChooser的方法名称。并且也在此方法的声明中。您可以在两者上使用JFileChooser2 JFileChooser的instid。
谢谢,我认为你需要一个,但似乎我错了。 –
关于[JavaDoc]的参数是什么(https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showDialog(java.awt.Component,%20java.lang。串))。用于放置相对于其父项的弹出窗口。无关它与环境有关。 – Compass