这是一个不完整的示例,但我想可以使您了解如何实现所需的目标。重要的一点是引用要在其中进行选择的属性,例如
YourDialog.this.selectedFile=file;
参见下面如何将其放置在代码中:
public class YourDialog extends JDialog implements ActionListener { protected File selectedFile=null; //Constructor public YourDialog(Jframe frame, boolean modal, String message) { //... create button and added to the panel someButton.addActionListener(new AbstractAction { public void actionPerformed(ActionEvent e) { final JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(YourDialog.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); // HERE IS THE TRICK I GUESS !! YourDialog.this.selectedFile=file; } } }); } }希望对您有所帮助,对不起您没有提供完整的示例。
编辑
本质上,我们没有将任何参数传递给AbstractAction。事实是AbstractAction可以通过访问like来访问“调用方”的任何非私有属性
YourDialog.this.somePropertyOrMethod。这是因为
AbstractAction是类的匿名
YourDialog类。



