我正在尝试从JFileChooser中加载一个文件,该文件位于JTabbedPane中。我希望文件加载在‘打开’按钮的点击,也显示在一个不同的窗格中的文件。我希望我能解释清楚。我有一个ActionListener,并尝试了一些东西,但我使用的代码似乎没有触发,因为它甚至不会打印到控制台。请你看看我的代码,看看我哪里出错了。谢谢
class listener implements ActionListener{
public void actionPerformed (ActionEvent e)
{.......// other actions
else if (e.getSource() instanceof JFileChooser){
JFileChooser openFile = (JFileChooser)e.getSource();
String command = e.getActionCommand();
if (command.equals(JFileChooser.APPROVE_SELECTION)){
File selectedFile = openFile.getSelectedFile();
System.out.print("test if working");
tp.setSelectedIndex(0); //Index of JTab I wand file to load
loadSavedGame(selectedFile);
}
else if (resume.equals(JFileChooser.CANCEL_OPTION)) {
//frame.setVisible(true);
tp.setSelectedIndex(0);
}
}
}
}发布于 2013-10-11 12:17:01
JFileChooser类有一个addActionListener(...)方法,它将接受上面的ActionListener。它不需要显示为弹出式,它的工作。
您从未告诉我们是否或如何将上面的ActionListener添加到JFileChooser中,但是如果您实际上正在这样做,而且您的代码仍然不工作,那么您将希望创建和发布一个斯考斯,以供我们测试和修复。
编辑
此外,我将创建仅与ActionListener一起使用的JFileChooser,从而去掉这一行:
else if (e.getSource() instanceof JFileChooser){如果监听器只添加到一个对象中,则不需要对源进行测试。
https://stackoverflow.com/questions/19317822
复制相似问题