1

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.

2 Answers2

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

I would recommend using Java Swing library instead of using the old Java AWT library, unless you have to.

EDIT for detail: The Swing library is much more portable than the AWT library. This is because it is a purely Java based GUI as opposed to AWT which uses most of the OS based GUI features. So, in terms of the OS, the swing library is just putting some pixels on the screen.

I find that the swing library is easier to use than AWT, although you do still have to use AWT for Listeners. I feel that it would be better for you to have a look into the swing API, as it should make it easier for you to do what you are trying.

  • this would be a lot more helpful to the OP if you explained your answer in more detail – nomistic May 24 '15 at 14:23
  • Thanks for your answer, but I need to use AWT for this course. Forgot to mention it, but edited the question already. – Valentin R. May 24 '15 at 14:49
  • I thought that might be your answer. I'll have another look and see if I can find a solution to the problem. – Clytomedes May 24 '15 at 15:01
  • The best solution that I could come up with is to set a Minimum Size so that the width is always greater than the length of the Menu. This means that you can still resize the window, you just cannot make it smaller than the size of the Menu Bar. I think the main problem is to do with pack and working with Menu Bar. But this should be an adequate solution. – Clytomedes May 24 '15 at 15:32