gpt4 book ai didi

java - 从控制台获取输入后,FileDialog 不显示

转载 作者:行者123 更新时间:2023-12-01 13:43:04 24 4
gpt4 key购买 nike

当我想在从控制台获得一些输入后打开文件对话框时,它失败了。请参阅下面的代码。当我第一次调用 openFileDialog 然后 chooseOption 时,它工作正常。有人知道这是怎么可能的吗?

public class SomeClass
{
int choice = 2;
Scanner keyboard;
String filter = "*.xml";

public void mainMenu() {
keyboard = new Scanner(System.in);
choice = ChooseOption();
FileDialog loadDialog = openFileDialog("Choose file", filter);

useInfo(loadDialog);
}

public int ChooseOption() {
System.out.println("Make your choice: \n"
+ "0) option A \n"
+ "1) option B");

try {
choice = keyboard.nextInt();
} catch(Exception e) {
System.out.println("Invalid input, try again.\n");
ChooseOption();
}
return choice;
}

public FileDialog openFileDialog(String title, String filter) {
System.out.println("Which file you want to use? \n");

FileDialog loadDialog = new FileDialog(new Frame(), title , FileDialog.LOAD);
loadDialog.setFile(filter);
//loadDialog.pack();
loadDialog.setVisible(true);

return loadDialog;
}

public void useInfo(FileDialog loadDialog) {
if( loadDialog == null || loadDialog.getFile() == null ) {
useDefaultFile();
doSomthing();
} else {
doSomthingElse();
}
}

}

最佳答案

FileDialog 显示为 frame,但它可能位于其他 Activity 窗口后面。

关于java - 从控制台获取输入后,FileDialog 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com