Search code examples
javaawtmenubar

Creating a Frame with MenuBar - Size not recognized by pack(), menubar is being cutoff


for an exercise I need to have a frame consisting of 2 buttons, and, if you click one, some text displaying. I need to also add a menubar. This works fine, it shows in the frame, but only the first menu. As I already found out, the problem is that using the pack() method, only the buttons size is considered, not the size of the menubar. And because the buttons are smaller than the menubar, it gets cutoff.

import java.awt.*;


public class Example extends Frame{

    private MenuBar menuBar;
    private Menu program;
    private Menu messageSettings;
    private MenuItem itmClose;
    private MenuItem 

    public Example() {
        menuBar = new MenuBar();
        program = new Menu("Programm");
        messageSettings = new Menu("Nachrichtenverwaltung");

        itmClose = new MenuItem("Schließen");
        itmWelcome = new MenuItem("Willkommen");

        setLayout(new BorderLayout());

        menuBar.add(program);
        menuBar.add(messageSettings);
        program.add(itmClose);
        messageSettings.add(itmWelcome);

        setMenuBar(menuBar);
        pack(); //this one doesn't show a window
        //setSize(400,600); //this one shows a Window
        setVisible(true);
    }

    public static void main(String args[]) {
        Example wnd = new Example();
    }

}

For this minimal example, I only showed the menubar, for me, this code now opens an empty window. If I uncomment the setSize, I see the whole menubar.

I would be very glad if someone could help me out and get this to work, using pack() or another method not using fixed values. I also have to use AWT for this course.


Solution

  • Without a Component that has a preferred size, your window's content pane is empty. As an example, I've added a small, colored Panel below. As an exercise, try removing the arbitrary size and adding a Component such as Label or Button; experiment with different layout managers on the Panel.

    image

    import java.awt.*;
    
    public class Example extends Frame {
    
        private MenuBar menuBar = new MenuBar();
        private Menu program = new Menu("Program");
        private Menu message = new Menu("Nachrichtenverwaltung");
        private MenuItem itmClose = new MenuItem("Schließen");
        private MenuItem itmWelcome = new MenuItem("Willkommen");
    
        public Example() {
            menuBar.add(program);
            menuBar.add(message);
            program.add(itmClose);
            message.add(itmWelcome);
            setMenuBar(menuBar);
            Panel panel = new Panel(){
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(320, 240);
                }
            };
            panel.setBackground(Color.cyan.darker());
            add(panel);
            pack(); 
            setVisible(true);
        }
    
        public static void main(String args[]) {
            Example wnd = new Example();
        }
    
    }
    

    Note that the platform pictured above moves the MenuBar to a place expected by users.