JFileChooser无法正常工作

时间:2015-07-23 13:50:09

标签: java swing jfilechooser

我正在使用JFileChooser,我希望在单击fileItem1时显示文件选择对话框。这是我的SwingMenu课程。

问题:showFileChooser方法中我错过了什么/做错了什么?提前谢谢。

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.io.File;
import java.awt.event.ActionListener;

/*
*This class encapsulates the build of the menu bar and is called from DrawPanelMain to add to the JFrame
 */
public class SwingMenu extends JMenuBar {

    /*
    * Initializes the JMenuItems that will be added to the menu.  This is necessary for
    * access by the ActionEvent handler.  Also includes FileChooser that will be used for
    * ActionEvent of clicking on fileItem1.
     */
    private JMenuItem fileItem1 = null;
    private JMenuItem fileItem2 = null;
    private JMenuItem editItem1 = null;
    private JMenuItem helpItem1 = null;
    private JMenuItem toolsItem1 = null;

    /*
    * Create JFileChooser to be used in ActionEvent for click on fileItem1
     */
    JFileChooser chooser = new JFileChooser("Desktop");

    /*
    *These will be the main items on the menuBar
    */
    public SwingMenu(){initMenuBar();}

            private void initMenuBar() {
                /*
                *Initializes for the main items on the menuBar
                 */
                JMenu fileMenu = new JMenu("File");
                JMenu editMenu = new JMenu("Edit");
                JMenu toolsMenu = new JMenu("Tools");
                JMenu helpMenu = new JMenu("Help");

                /*
                *Initializes for the components of the main items
                 */
                JMenuItem fileItem1 = new JMenuItem("Open");
                JMenuItem fileItem2 = new JMenuItem("Save");
                JMenuItem editItem1 = new JMenuItem("Edit Configuration");
                JMenuItem helpItem1 = new JMenuItem("User Manual");
                JMenuItem toolsItem1 = new JMenuItem("Fetch Configuration");

                /*
                 * Add action listener to fileItem1 for use in JFileChooser ActionEvent
                 */
                fileItem1.addActionListener(new ActionListener() {
                    public void showFileChooser (ActionEvent e){
                        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                        FileNameExtensionFilter filter = new FileNameExtensionFilter("Directories and XML files", "xml");
                        chooser.setFileFilter(filter);
                        int returnVal = chooser.showOpenDialog(this);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            String path = chooser.getSelectedFile().getAbsolutePath();
                            JOptionPane.showMessageDialog(null, "You selected" + path);
                        }
                    }
                });

                /*
                *Each component is added to the assigned menu item
                 */
                fileMenu.add(fileItem1);
                fileMenu.add(fileItem2);
                editMenu.add(editItem1);
                toolsMenu.add(toolsItem1);
                helpMenu.add(helpItem1);

                /*
                *Menu items are added to the menuBar
                 */
                add(fileMenu);
                add(editMenu);
                add(toolsMenu);
                add(helpMenu);
            }
}

1 个答案:

答案 0 :(得分:2)

问题在于:public void showFileChooser (ActionEvent e){。在Java中,事件处理方法必须是public void actionPerformed(ActionEvent e) {

                fileItem1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                        FileNameExtensionFilter filter = new FileNameExtensionFilter("Directories and XML files", "xml");
                        chooser.setFileFilter(filter);
                        int returnVal = chooser.showOpenDialog(this);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            String path = chooser.getSelectedFile().getAbsolutePath();
                            JOptionPane.showMessageDialog(null, "You selected" + path);
                        }
                    }
                });