我想为 JFileChooser 按钮使用抽象操作,因为会有很多这样的按钮。
public class OpenFileAction extends AbstractAction {
JFrame frame;
JFileChooser chooser;
OpenFileAction(JFrame frame, JFileChooser chooser) {
super("Open...");
this.chooser = chooser;
this.frame = frame;
}
public void actionPerformed(ActionEvent evt) {
// Show dialog; this method does not return until dialog is closed
chooser.showOpenDialog(frame);
}
};
显然我想将 JFileChooser 结果写入一个变量。操作完成后如何访问 e.getSource() ?这不起作用,因为它是在 FileChooser 对话框打开之前触发的:
JButton btnNewButton_1 = new JButton(new OpenFileAction(new JFrame(), new JFileChooser(new File(".")) ) );
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//process e.getSource() ?
}
});