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.