从控制台获取输入后不显示文件对话框



当我从控制台获得一些输入后想要打开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();
    }
}
}

FileDialog显示为frame,但它可能落后于其他Active Windows.

以下代码使用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();
    }
}

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

相关内容

  • 没有找到相关文章

最新更新