我已经在我的命令行程序中实现了jFileChooser,并且可以正常工作,就像只有一个烦人的问题一样。似乎它在每个窗口下打开,没有任何警报。实际上,起初我什至错过了几次,这使我相信自己实施错误。
我已经实现了如下:
System.out.println("Please select the file");
JFileChooser fc = new JFileChooser();
int retValue = fc.showOpenDialog(new JPanel());
if(retValue == JFileChooser.APPROVE_OPTION){
g.inputFile = fc.getSelectedFile();
}else {
System.out.println("Next time select a file.");
System.exit(1);
}
本质上,我只希望jFileChooser可以使用户选择一个文件作为输入文件。这是唯一需要GUI实现的组件,因此,如果我可以避免编写GUI,那将很有帮助。
因此,在尝试了来自不同堆栈溢出主题的各种操作之后,我最终得到了在Windows 7的每个窗口上方始终可靠地打开的结果。
public class ChooseFile {
private JFrame frame;
public ChooseFile() {
frame = new JFrame();
frame.setVisible(true);
BringToFront();
}
public File getFile() {
JFileChooser fc = new JFileChooser();
if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)){
frame.setVisible(false);
return fc.getSelectedFile();
}else {
System.out.println("Next time select a file.");
System.exit(1);
}
return null;
}
private void BringToFront() {
frame.setExtendedState(JFrame.ICONIFIED);
frame.setExtendedState(JFrame.NORMAL);
}
}
正如它在我的程序中所代表的那样,它是一个内部类,并通过调用来调用:
System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句