Java 我是否超载了actionListener?文件选择器

Java 我是否超载了actionListener?文件选择器,java,actionlistener,jfilechooser,Java,Actionlistener,Jfilechooser,我正在尝试使用actionListener从JFileChooser加载保存的文件。下面是一段代码 class chooserListener implements ActionListener{ public void actionPerformed (ActionEvent e) { if (e.getSource() instanceof 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;
    }
类选择器Listener实现ActionListener{
已执行的公共无效操作(操作事件e)
{   
if(例如,JFileChooser的getSource()实例){
JFileChooser openFile=(JFileChooser)e.getSource();
String command=e.getActionCommand();
if(command.equals(JFileChooser.APPROVE_SELECTION)){
File selectedFile=openFile.getSelectedFile();
LoadSavedName(选择的文件);
系统输出打印(“单击打开文件”);
tp.setSelectedIndex(0);
}
else if(command.equals(JFileChooser.CANCEL_SELECTION)){
系统输出打印(“测试仪”);
tp.setSelectedIndex(0);
}
}
}
}
addActionListener(新的chooserListener());
public void loadSavedGame(文件loadfile){
int allCells=countCells(加载文件);
setMineGame(所有细胞);
试一试{
扫描仪加载文件=新扫描仪(加载文件);
while(loadFile.hasNextInt()){
for(int i=0;i

我觉得我有太多的方法让动作听众去做。当我单击“打开”和测试打印行“System.out.print”(“单击打开文件”);时,它将挂起不打印。我的其余代码非常大,我不知道如何创建SSCE(?)。我想知道是否有人能明白为什么我的actionListener会被绞死?感谢IA

它似乎需要花费大量时间才能执行。由于此方法在中运行,您感觉您的程序正在挂起,并且从未到达
System.out.print(“单击打开文件”)行。我将在一个单独的测试用例中开始测试这个方法的响应时间

不管怎样,我给你提几个建议:

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
方法中发布一个.while
循环时,
loadFile.close()
需要在
之外,而
mineGame.repain()
可能也应该是。谢谢@VGR,我尝试了这个方法,但actionListener仍然挂起:-(.我甚至不知道从何处开始分解它以使SSCCE工作,但感谢您的输入。非常感谢您的代码,我仍在学习集合和数组列表,因此根本不知道如何实现它。但我复制了您的代码,它工作得非常好!唯一的一点是选择器应该位于选项卡内,它是现在没有显示在选项卡中。我认为这与“chooser.showOpenDialog(null)”有关。我已将选择器添加到选项卡中(在索引[1])。您能给我进一步的指导,让选择器返回到选项卡中吗?还是我应该问一个新问题?再次感谢:-)不客气!我很高兴它能为您工作:-)对不起,我以前没有在
JPanel
中使用
JFileChooser
。注意:此组件通常用于显示单独的模式对话框,以强制用户在继续之前进行选择。我想你可以添加一个
JMenu
而不是一个
JMenuItem
名为Load saved game。。。并在用户单击此选项时显示
JFileChooser
。这是游戏中非常常见的设计。
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);
}