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

2

执行起来似乎loadSavedGame(File file)需要很多时间。由于此方法在Event Dispatch Thread中运行,您会觉得您的程序挂起并且永远不会到达System.out.print("clicked open file");线路。我将在单独的测试用例中开始测试此方法的响应时间

无论如何,我会建议你一些提示:

1)请注意,无需实现 anActionListener即可执行您的代码。你可以简单地做到这一点:

JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog(null);
if(returnValue == JFileChooser.APPROVE_OPTION){
    //make stuff if approved
} else if(returnValue == JFileChooser.CANCEL_OPTION){
    //make stuff if canceled
}

我认为它使人们的生活更轻松。

2)另一方面,请注意您有两个 I/O 操作:通过countCells(File countCell)方法获取单元格计数并在方法中获取单元格本身loadSavedGame(File loadfile)。您可以更好地读取文件一次:

public List<Integer> getCells(File file){
    List<Integer> list = new ArrayList<>();
    try {    
        Scanner getCells = new Scanner(file);
        while (getCells.hasNextInt()){
            list.add(Integer.valueOf(getCells.nextInt()));

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

并在方法中进行此更改loadSavedGame

public void loadSavedGame(File loadfile) {

    List<Integer> allCells = getCells(loadfile);
    setMineGame(allCells.size());
    int index = 0;

    for(Integer value : allCells){
        mineGame.setCell(index, value);
        index++;
    }

    mineGame.repaint();
    tp.setSelectedIndex(0);
}
于 2013-10-21T11:40:27.480 回答