2016-07-27 34 views
-1

我有游戏程序,我希望玩家选择文件目录来保存他的游戏。我正在使用JFileChooser这个作品完美的罚款,但它不会关闭。Java 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); 
} 

我认为空间是真实的,它使执行代码的每一打勾转出它不会停止阅读它停止滴答proccess的代码。它不断地阅读这个方法。其他答案使用面板,我没有使用面板在这个程序中我正在使用canvasjframe只有

回答

1

如果这是你正在寻找的只是能够选择文件后关闭文件选择器对话框或保存文件。只是仍然尝试使代码更具说明性,我认为这应该基于我对你解释的内容的理解

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