PulsePanda
PulsePanda

Reputation: 1856

Why doesnt my JButton always pop up on a GUI?

So im making a game, and i want a jbutton to pop up in the window and when you click it you can log in. the problem is that when i start the game, it doesnt always pop up, which is kind of annoying. the only think would be the problem is the order im doing things. here is the code:

public static void createWindow() {
    ImagePanel panel = new ImagePanel(
            new ImageIcon(backgroundFile).getImage()); //used for the background


    JButton login = new JButton(new AbstractAction("Login") {
        public void actionPerformed(ActionEvent e) {
            Login.createWindow();
        }
    });
    login.setBounds(300, 300, 100, 100);


    frame.getContentPane().add(panel); //sets the background to a pic
    frame.setJMenuBar(MenuBar.menuBarCreator()); creates the menu bar
    frame.pack();
    frame.setResizable(false);
    frame.setTitle("*Game Title* Beta 0.0.1 ADMINISTRATOR VERSION");
    frame.setSize(ImagePanel.img.getWidth(null),
            ImagePanel.img.getHeight(null));
    frame.setLocation(Monitor.setLocationHeight(),
            Monitor.setLocationWidth());
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    StreamingLineSound.start(soundFile); //starts a music file
    frame.add(login);

}

any help would be fantastic. so basically all i want is an idea why it doesnt pop up all the time. thanks

Upvotes: 2

Views: 126

Answers (1)

mKorbel
mKorbel

Reputation: 109813

Code line frame.setVisible(true); must be last line in the public static void createWindow() {, because you display JFrame and thenafter add frame.add(login);

Upvotes: 3

Related Questions