简体   繁体   中英

How do I make a swing menu bar that is present at the top of any JFrame I've created?

So I'm creating an application where different JFrames may be opened at any given time. I've created a menu bar at the top of my main window, your standard "File, Tools, Edit, etc..." menu bar. I was wondering how I get this menu bar to be at the top of all the JFrames and not just the main application window frame without having to copy and paste all the code for it on every single frame.

As @mwarren and @jhamon said in comments you have to use inheritance. Create a Subclass of your JFrame and implement all the required functionality for the MenuBar there. I had the same issues on my diploma thesis and I did the following:

I created a class, let's say ExtendableJFrame, and created the MenuBar there. Also there I implemented methods that enable or disable buttons/menus. So when I was creating a new JFrame, the menu bar was initialised and then, using one line of code I was able to enable or disable anything I wanted (buttons/menus).

If you add shortcuts make sure that they don't work when buttons are disabled!

Moreover, I had to do similar things using the menu bar, but not exactly the same , in different JFrames. So, I created an Interface , let's say MyMenuBarInterface, and then implement the code in each JFrame classes separately giving them the exact functionality I wanted.

Use the pattern factory . The code will looks like this:

import java.awt.FlowLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
 * <code>FactoryDemo</code>.
 */
public class FactoryDemo {

    private int frameIndex;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FactoryDemo()::createUI);
    }

    // Factory method to create your editor frame
    private JFrame createEditor(String title) {
        JFrame frm = new JFrame(title);
        frm.setJMenuBar(createMenu());
        frm.add(new JScrollPane(new JTextArea(10, 40)));
        frm.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        frm.pack();
        return frm;
    }

    // factory method to create your menu bar
    private JMenuBar createMenu() {
        JMenuBar menu = new JMenuBar();
        menu.add(new JMenu("File"));
        menu.add(new JMenu("Edit")); // so on
        return menu;
    }

    private void createUI() {
        // create main frame
        JFrame frm = new JFrame();
        JButton button = new JButton("Add Editor");
        button.addActionListener(e -> {
            Point location = frm.getLocation();
            JFrame nextFrm = createEditor("Editor: " + (++frameIndex));
            location.x += frameIndex * 20;
            location.y = frameIndex * 20;
            nextFrm.setLocation(location);
            nextFrm.setVisible(true);
        });
        frm.setLayout(new FlowLayout());
        frm.add(button);
        frm.setSize(300, 200);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM