JavaJFileChooser不会死

JavaJFileChooser不会死,java,swing,jfilechooser,Java,Swing,Jfilechooser,我有一个游戏程序,我想让玩家选择文件目录来保存他的游戏。我正在使用JFileChooser,因为它工作得非常好,但不会关闭 代码如下: public NewGameState(Game game) { super(game); open = new JButton(); fc = new JFileChooser(); fc.setCurrentDirectory(new java.io.File(".")); fc.setDialogTitle

我有一个游戏程序,我想让玩家选择文件目录来保存他的游戏。我正在使用
JFileChooser
,因为它工作得非常好,但不会关闭

代码如下:

    public NewGameState(Game game) {
    super(game);

    open = new JButton();
    fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("."));
    fc.setDialogTitle("Choose file for the new world");
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

}

public void tick() {

    input();
    selection();
}

public void render(Graphics g) {

    drawSelection(g);
    drawGUI(g);
}

private void input(){

    if(game.getKeyManager().up){

        selection --;
    }else if(game.getKeyManager().down){

        selection ++;
    }

    if(game.getKeyManager().space){

        if(selection == 0){

            fileChooser();      //calling fileChooser method when pressing space
        }else if(selection == 1){

        }else if(selection == 2){


        }
    }
}

private void selection(){
    //blahbahabahaa
    }
}

//fileChooser
private void fileChooser(){

    fc.showOpenDialog(null);
}

我认为空格是正确的,它会一直执行代码,每次勾号,它不会停止读取代码,从而停止勾号过程。它不断地反复阅读这个方法。其他答案是使用panel,我在本程序中没有使用panel,我使用的是
canvas
jframe
,如果这是您正在寻找的,只是为了在选择文件或保存文件后能够关闭文件选择器对话框。还是尽量让代码更具解释性,我认为这应该基于我对你解释的理解

 int c = fileWindow.showSaveDialog(this);
        if(c==JFileChooser.APPROVE_OPTION)
            files.append(fileWindow.getSelectedFile()+"\n");