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

时间:2013-12-12 15:42:57

标签: java console user-input filedialog

当我想从控制台获得一些输入后打开FileDialog时,它会失败。 请参阅下面的代码。 当我第一次呼叫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();
    }
}

}

2 个答案:

答案 0 :(得分:1)

FileDialog显示为frame,但可能位于其他Active Windows.

之后

答案 1 :(得分:0)

以下代码使用jdk6,

import java.awt.FileDialog;
import java.awt.Frame;
import java.util.Scanner;

public class Dialog {

    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.setModal(true);
        //loadDialog.pack();
        loadDialog.setVisible(true);

        return loadDialog;
    }

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

    public static void main(String[] args) {
        Dialog d = new Dialog();
        d.mainMenu();
    }
}

当显示对话框时,很可能它位于其他活动窗口后面。