0

我想为 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() ?
        }
    });
4

3 回答 3

3

我认为您所追求的是以下内容:

public abstract class OpenFileAction extends AbstractAction {
    JFrame frame;
    JFileChooser chooser;

    public OpenFileAction(JFrame frame, JFileChooser chooser) {
        super("Open...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        int option = chooser.showOpenDialog(this.frame);
        if (option == JFileChooser.APPROVE_OPTION) {
            File selectedFile = chooser.getSelectedFile();
            doWithSelectedFile(selectedFile)
        }
    }

    /**
     * Method to override, which gets called with the selected file.
     */
    protected abstract doWithSelectedFile(File file);
}

...

OpenFileAction action = new OpenFileAction(frame, new JFileChooser(new File("."))) {
    @Override
    protected void doWithSelectedFile(File file) {
        // do what you want here
    }
};
JButton button = new JButton(action);
于 2012-06-29T07:47:03.020 回答
1

这是您的代码更新,展示了如何解决这个问题。不需要您的动作侦听器,因为您已经Action在 J​​Button(它也是一个 ActionListener)上设置了一个。

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestFileChooser {

    public static class OpenFileAction extends AbstractAction {
        JFileChooser chooser;

        OpenFileAction(JFileChooser chooser) {
            super("Open...");
            this.chooser = chooser;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            int retval = chooser.showOpenDialog((Component) evt.getSource());
            if (retval == JFileChooser.APPROVE_OPTION) {
                File selectedFile = chooser.getSelectedFile();
                JOptionPane.showMessageDialog((Component) evt.getSource(), "You have chosen " + selectedFile.getAbsolutePath());
            }
        }
    };

    protected void initUI() {
        JFrame frame = new JFrame("Test file chooser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser chooser = new JFileChooser(new File("."));
        JButton btnNewButton_1 = new JButton(new OpenFileAction(chooser));
        frame.add(btnNewButton_1);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestFileChooser().initUI();
            }
        });
    }
}

这是关于JFileChooser的教程,它可能会提供更多信息。

于 2012-06-29T07:26:25.410 回答
0

编辑:

以下代码适合您:

JButton openButton = new JButton();
openButton.addActionListener(new ActionListener(){
    public void actionPerformed(evt){
        final JFileChooser fileChooser = new JFileChooser();
        File openFile;
        if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            openFile= fileChooser.getSelectedFile();
        //place your code here
    }
});

(如果您绝对需要本地方法)

JButton openButton = new JButton();
openButton.addActionListener(new ActionListener(){
    public void actionPerformed(evt){
        final JFileChooser fileChooser = new JFileChooser();
        if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            openButtonActionPerformed(fileChooser.getSelectedFile());
    }
});
openButtonActionPerformed(File openFile){
    //place your code here
}

无需创建另一个类

于 2012-06-29T07:53:49.790 回答