1

actionListener を使用して JFileChooser から保存済みファイルをロードしようとしています。コードのスニペットを次に示します。

class chooserListener implements ActionListener{
            public void actionPerformed (ActionEvent e)
            {   
                if (e.getSource() instanceof JFileChooser){
                    JFileChooser openFile = (JFileChooser)e.getSource();
                    String command = e.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)){
                        File selectedFile = openFile.getSelectedFile();

                        loadSavedGame(selectedFile);
                        System.out.print("clicked open file");
                        tp.setSelectedIndex(0);
                    }
                    else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.print("tester");
                          tp.setSelectedIndex(0);
                    }
                }
            }
        }
chooser.addActionListener(new chooserListener());

public void loadSavedGame(File loadfile) {

        int allCells = countCells(loadfile);
        setMineGame(allCells);

        try {
            Scanner loadFile = new Scanner(loadfile);
            while (loadFile.hasNextInt()){
                for (int i = 0; i < allCells; i++){
                    mineGame.setCell(i, loadFile.nextInt());
                    //System.out.print("loading saved game");  
                }
                loadFile.close();
                mineGame.repaint();
                tp.setSelectedIndex(0);
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

private int countCells(File countCell) {

    int cellCount = 0;

    try {
        Scanner getCells = new Scanner(countCell);
        while (getCells.hasNextInt()){
            cellCount++;

        }
        getCells.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.print(cellCount);
    return cellCount;
}

public void setMineGame(int cells) {
    game.removeAll();
    mineGame.setDifficulty(cells);
    mineGame = new Board(statusbar, difficulty);
    game.add(mineGame, BorderLayout.CENTER);
    game.add(statusbar, BorderLayout.SOUTH);

    frame.validate();
    frame.repaint();

}   
public void setDifficulty(int cells){

        if(cells == 256){
            difficulty = 0;
        }
        if (cells == 676){
            difficulty = 1;
        }
        else difficulty = 2;
    }

アクションリスナーが行うメソッドが多すぎるように感じます。「開く」をクリックするとハングし、テスト印刷行「System.out.print("clicked open file");」印刷しません。私のコードの残りの部分は非常に大きく、SSCE(?) の方法がわかりません。私のactionListenerがハングしている理由を誰かが見ることができるかどうか疑問に思っていますか? ありがとうIA

4

1 に答える 1