我超载了我的actionListener吗? JFileChooser中

时间:2013-10-21 10:57:36

标签: java actionlistener jfilechooser

我正在尝试使用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(“点击打开文件”);'不打印。我的其余代码非常大,我不知道如何使用SSCE(?)。我想知道是否有人能看出我的actionListener挂起的原因?谢谢IA

1 个答案:

答案 0 :(得分:2)

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

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

1)注意,不需要实现ActionListener来执行代码。你可以简单地做到这一点:

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);
}