5

我有以下 SwingMenu 类。包装基地;

import javax.swing.*;

public class SwingMenu {
    public static void main(String[] args) {
        SwingMenu s = new SwingMenu();
    }

    public SwingMenu() {
        JFrame frame = new JFrame(
                "Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);
        frame.setJMenuBar(menubar);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

我想通过从这个主类调用它来显示菜单。

package base;

import javax.swing.*;

import java.awt.*;
import base.SwingMenu;

public class StickyNotes {

    private static void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");

        frame.setPreferredSize(new Dimension(5000, 5000));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        SwingMenu mainBar = new SwingMenu();
        //frame.setJMenuBar(mainBar);
        //frame.getContentPane().add(mainBar);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我整个早上都想不通 :) 如何让菜单栏显示在 Java Swing 中?

    // Add Main Menu
    SwingMenu mainBar = new SwingMenu();
4

3 回答 3

4

您正在创建 2 个不同JFrame的 s。创建后JFrame

JFrame frame = new JFrame("Java Sticky Notes");

创建菜单栏并将其分配给JFrame

JMenuBar menubar = new JMenuBar();

// ...

frame.setJMenuBar(menubar);

不需要:

SwingMenu mainBar = new SwingMenu();
于 2012-07-13T17:08:22.963 回答
4

试试这个:

这基本上制作SwingMenu了一个菜单栏,有助于封装菜单栏的构建。

public class SwingMenu extends JMenuBar{
    public SwingMenu() {
        JMenu filemenu = new JMenu("File");
        JMenu editmenu = new JMenu("Edit");

        // Build your file menu and edit menu here...

        add(filemenu);
        add(editmenu);
    }
}

现在,您createAndShowGUI()只需创建一个表单并将新创建的菜单栏添加到其中。

private static void createAndShowGUI() {
     // Create and set up the window.
     JFrame frame = new JFrame("Java Sticky Notes");
     // other stuff...
     // Add Main Menu
     SwingMenu mainBar = new SwingMenu();
     frame.setJMenuBar(mainBar);
     // Display the window.
     frame.pack();
     frame.setVisible(true);
}
于 2012-07-13T17:08:49.690 回答
4

检查这个修改后的代码示例:

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private JMenuBar getMenuBar()
    {
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);

        return menubar;
    }

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        frame.setJMenuBar(getMenuBar());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

或者你可以像这样修改你的代码,如果你真的想把设置保留JMenuBar在一个不同的类中,你可以简单地创建一个 ObjectSwingMenu Class并通过创建这个 Class 的一个 Object 来调用该方法getMenuBar()

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        SwingMenu swingMenu = new SwingMenu();
        frame.setJMenuBar(swingMenu.getMenuBar());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

class SwingMenu {

    public JMenuBar getMenuBar() 
    {   
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);

        return menubar;
    }
}
于 2012-07-13T17:12:04.920 回答