有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JMenu栏未出现在GUI中

这是一个新手问题,但我正在努力让这个JMenu栏显示在拆分窗格上方。有人能帮我解释一下我做错了什么吗?据我所知,我添加了JMenu及其下拉菜单

非常感谢您为我的问题提供的解决方案和帮助

以下是我的代码:

public class JavaAssignmentPanel {

    JMenuBar setupMenu() {

        JMenuBar menuBar = new JMenuBar(); //menubar
        JMenu menu1 = new JMenu("Menu"); //menu
        menuBar.add(menu1); //add menu to gui
        JMenuItem menuItem1 = new JMenuItem("Item 1", KeyEvent.VK_1); //create drop down menu
        menu1.add(menuItem1); //adds drop down menu to gui

        //execute code when selected
        menuItem1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                //  panel.showText("Example1 text - normally read from file");
            }
        });

        return menuBar;
    }

    public static void main(String[] args) throws FileNotFoundException {

        window window = new window();

    }

    private static class window extends JFrame {

        public window() throws FileNotFoundException {

            JPanel leftScrollPane = new JPanel();
            JPanel rightPane = new JPanel();
            JSplitPane splitPane;

            this.setVisible(true);
            this.setSize(400, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            splitPane = new JSplitPane();
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setDividerSize(10);
            splitPane.setDividerLocation(100);
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(200);

            Dimension minimumSize = new Dimension(100, 50);

            leftScrollPane.setSize(400, 400);

            splitPane.setPreferredSize(new Dimension(400, 200));
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            this.add(splitPane);

        }
    }

}

共 (4) 个答案

  1. # 1 楼答案

    必须在JFrame中设置JMenuBar

    查看代码,您可以通过将setupMenu方法设置为静态,并在window类中调用它,如下所示:

    this.setJMenuBar(setupMenu());
    

    代码:

    public class JavaAssignmentPanel {
    
    // making this method as static
    static JMenuBar setupMenu() {
    
        JMenuBar menuBar = new JMenuBar(); // menubar
        JMenu menu1 = new JMenu("Menu"); // menu
        menuBar.add(menu1); // add menu to gui
        JMenuItem menuItem1 = new JMenuItem("Item 1", KeyEvent.VK_1); // create
                                                                        // drop
                                                                        // down
                                                                        // menu
        menu1.add(menuItem1); // adds drop down menu to gui
    
        // execute code when selected
        menuItem1.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
                // panel.showText("Example1 text - normally read from file");
            }
        });
    
        return menuBar;
    }
    
    public static void main(String[] args) throws FileNotFoundException {
    
        window window = new window();
    
    }
    
    private static class window extends JFrame {
    
        public window() throws FileNotFoundException {
    
            JPanel leftScrollPane = new JPanel();
            JPanel rightPane = new JPanel();
            JSplitPane splitPane;
    
            this.setVisible(true);
            this.setSize(400, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // calling setupMenu method to set the JMenuBar in JFrame
            this.setJMenuBar(setupMenu());
    
            splitPane = new JSplitPane();
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setDividerSize(10);
            splitPane.setDividerLocation(100);
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(200);
    
            Dimension minimumSize = new Dimension(100, 50);
    
            leftScrollPane.setSize(400, 400);
    
            splitPane.setPreferredSize(new Dimension(400, 200));
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            this.add(splitPane);
    
        }
    }}
    
  2. # 2 楼答案

    我查看了您的代码,并做了以下更改:

    向setupMenu()添加了一个修饰符(

    public static JMenuBar setupMenu() { ... }
    

    通过调用JFrames构造函数中的this.setJMenuBar(setupMenu());将菜单栏添加到JFrame中

    我还将JFrame的大小增加到1280x720以查看更改

    This是我所做的所有更改的类

    我还鼓励你总是根据Java Code Conventions来命名你的类,它是驼色大小写,以大写字母(Uppper Camel Case)开头。这使你的代码更容易阅读

  3. # 3 楼答案

    定义方法setupMenu(),该方法返回JMenuBar。但菜单栏必须添加到窗口中

    它可以通过

    this.setJMenuBar(setupMenu());
    

    作为window()构造函数中的最后一个命令(即this.add(splitPane);之后)

  4. # 4 楼答案

    所以我可能完全失明了,但我看不到你在班上其他任何地方调用setupMenu方法。你需要告诉程序你正在给jframe添加一个菜单栏