如何打开文件弹出 [英] How to get open file popup
本文介绍了如何打开文件弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pre $
public static void main(String [] args)throws IOException {
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog(null);
if(returnValue == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
}
//我不希望这是硬编码的:
String filePath =/Users/Bill/Desktop/hello.txt;
我应该如何去做这件事?
文件文件的范围。
尝试在if块之外声明文件
。
文件file =空值;
if(returnValue == JFileChooser.APPROVE_OPTION){
file = chooser.getSelectedFile();
if(file!= null)
{
String filePath = file.getPath();
}
Right now, I have a set class path, but I want to have an open file pop up and the user chooses which file to open. I've tried JFileChooser, but haven't been successful so far. Here's my code:
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog( null ) ;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
File file = chooser.getSelectedFile() ;
}
// I don't want this to be hard-coded:
String filePath = "/Users/Bill/Desktop/hello.txt";
How should I go about doing this?
解决方案
I think the problem is the scope of File file
.
Try Declaring file
outside the if-block.
File file = null;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
file = chooser.getSelectedFile() ;
}
if(file != null)
{
String filePath = file.getPath();
}
这篇关于如何打开文件弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文