我的一个非常简单的文件选择对话框实现似乎存在问题,每次都需要将Netbeans最小化才能访问它,这让我感到相当沮丧,特别是在进行测试时。
我在网上看到了一些解决方案,包括SO,但似乎都不起作用,而另一些解决方案对于我目前的水平来说过于冗长和复杂。
private void fileSearch() {
JFileChooser fileSelect = new JFileChooser();
int returnVal = fileSelect.showOpenDialog(null);
String pathToFile;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileSelect.getSelectedFile();
pathToFile = file.getAbsolutePath();
try {
P.binaryFileToHexString(pathToFile);
} catch (Exception e) {
System.out.print("Oops! there was an error there..." + e);
}
System.out.println("\nYou chose to open this file: " + file.getName());
}
}
我的一些尝试包括使用;
.requestFocus();
.requestFocusInWindow();
.setVisible();
有没有特定的属性/方法可以设置来解决这个问题?
java.io.File
、java.util.Scanner
和javax.swing.JFileChooser
。 - Carlos