Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFileChooser和ActionEvent处理程序的放置位置_Java_Swing_Jfilechooser - Fatal编程技术网

Java JFileChooser和ActionEvent处理程序的放置位置

Java JFileChooser和ActionEvent处理程序的放置位置,java,swing,jfilechooser,Java,Swing,Jfilechooser,我在创建JFileChooser和关联的ActionEvent以单击下面代码中的fileItem1时遇到了一些问题 问题:JFileChooser应该在哪里声明,ActionEvent方法应该在哪里 import javax.swing.*; /* * This class encapsulates the build of the menu bar and * is called from DrawPanelMain to add to the JFrame */ public cla

我在创建JFileChooser和关联的ActionEvent以单击下面代码中的fileItem1时遇到了一些问题

问题:JFileChooser应该在哪里声明,ActionEvent方法应该在哪里

import javax.swing.*;

/*
 * 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;

    /*
     * 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");

        /*
         * 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);
    }
}

必须向fileItem1添加ActionListener,如下所示:

fileItem1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        JFileChooser chooser = new JFileChooser(null);
        if (chooser.showOpenDialog() != JFileChooser.CANCEL_OPTION) {
            // ...
        }
    }
});

谢谢这在SwingMenu或InitMenuBar的范围内吗?@feltersnach两者都在,取决于您想要实现什么