Falcon1492
Falcon1492

Reputation: 23

JMenuBar is / is not Displayed in sucsessive runs

Hello guys I have used this page already many times passively because there are many quite good explenations, but today it is so far that I have to ask a question myself since it is in my opinion quite weird what is going on.

I programmed a litte game using a JFrame from Javax.Swing with a JMenu included. The program works actually quite fine and the JMenuBar is also working and displayed. However when I run the programm it might happen that the menu is not displayed although it is actually there (I can use the shortcuts). When the menu is displayed, seems to me more or less arbitrary. I don't change one bit of the code and there are no warnings or errors in eclipse, nevertheless in one time I can see the menu bar another time not.

So far I couldn't see any pattern of when this occours. I can only suspect it has something to do with how I close the Frame/program and things happening due to my OS in the background.

Windows 7 Ultimate SP1 Eclipse Java Development Tools Version: 3.10.1.v20150204-1700 Java version 1.8.0_45

Here is my code of the Class genereting the JFrame: then there is some stuff in the actionhandler to start the game etc. and a main class to call the constructor of the Frame class.

public class Frame extends JFrame implements ActionListener{    

private Screen s;
public Frame()
{
    //---set frame
    this.setUndecorated(false);
    this.setTitle("Super Tick Tack Toe");
    ImageIcon frameIcon = new ImageIcon(Frame.class.getResource("res/STTT_icon_64x64.png"));
    this.setIconImage(frameIcon.getImage());
    this.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setResizable(false);
    this.setSize(windowWidth,windowHeight);
    this.setLocationRelativeTo(null);

    //---game Menu
    //---exit
    JMenuItem exit = new JMenuItem("exit");
    KeyStroke escKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
    exit.setAccelerator(escKeyStroke);
    exit.addActionListener(this);
    //---load
    JMenuItem save = new JMenuItem("save game");
    KeyStroke saveKeyStroke = KeyStroke.getKeyStroke("control S");
    save.setAccelerator(saveKeyStroke);
    save.addActionListener(this);
    //---save
    JMenuItem load = new JMenuItem("load game");
    KeyStroke loadKeyStroke = KeyStroke.getKeyStroke("control L");
    load.setAccelerator(loadKeyStroke);
    load.addActionListener(this);
    //---preferences
    JMenuItem preferences = new JMenuItem("preferences");
    preferences.addActionListener(this);
    JMenuItem newGame = new JMenuItem("new game");
    KeyStroke newGameKeyStroke = KeyStroke.getKeyStroke("F2");
    newGame.setAccelerator(newGameKeyStroke);
    newGame.addActionListener(this);
    //---pack game
    JMenu game = new JMenu("game");
    game.add(newGame);
    game.add(new JSeparator()); // SEPARATOR
    game.add(save);
    game.add(load);
    game.add(new JSeparator()); // SEPARATOR
    game.add(preferences);
    game.add(new JSeparator()); // SEPARATOR
    game.add(exit);

    //---help Menu
    //---about
    JMenuItem about = new JMenuItem("about STTT");
    about.addActionListener(this);
    KeyStroke aboutKeyStroke = KeyStroke.getKeyStroke("F12");
    about.setAccelerator(aboutKeyStroke);
    //---rules
    JMenuItem rules = new JMenuItem("rules");
    rules.addActionListener(this);
    KeyStroke rulesKeyStroke = KeyStroke.getKeyStroke("F1");
    rules.setAccelerator(rulesKeyStroke);
    //---pack help
    JMenu help = new JMenu("help");
    help.add(rules);
    help.add(new JSeparator()); // SEPARATOR
    help.add(about);

    //---pack menu bar
    JMenuBar mb = new JMenuBar();
    mb.add(game);
    mb.add(help);
    this.setJMenuBar(mb);

    s = new Screen();
    s.setBounds(0, 0, windowWidth, windowHeight);
    add(s);
}
public void repaint()
{
    s.repaint();
}
private class Screen extends JLabel{
    private static final long serialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics gg)
    {
        super.paintComponent(gg);
        if(SuperTickTackToe.playing){
            Graphics2D g = (Graphics2D) gg;
            STTTgame.draw(g);
        }
    }
}

Upvotes: 2

Views: 44

Answers (1)

camickr
camickr

Reputation: 324207

this.setVisible(true);
this.setResizable(false);
this.setSize(windowWidth,windowHeight);
this.setLocationRelativeTo(null);

Don't invoke the above code at the start of the constructor.

You should invoke setVisible(true) as the last statement in the constructor of your frame, AFTER all components have been created and added to the frame.

s = new Screen();
s.setBounds(0, 0, windowWidth, windowHeight);
add(s);

this.setResizable(false);
this.setSize(windowWidth,windowHeight);
this.setLocationRelativeTo(null);
this.setVisible(true);

Also, how can the size of the Screen and the JFrame be the same size, since a JFrame contains a title bar and Borders? You really should just be using the pack() method of the frame to determine its size. Then the preferred size of the components you add to the frame will be used to determine the frame size.

Upvotes: 2

Related Questions