简体   繁体   中英

JButtons don't show until I minimize and bring the window up again

I have on more than one computer a problem (meaning it isn't just one computer giving me the problem). I use Eclipse as my IDE for Java, and I can't seem to work out the answer to my problem. I have added several JButtons to my video game's launcher, and when I debug the project, the buttons don't appear until I either slide my mouse over them, or minimize and reopen the window. I have exported the project and mostly the buttons work, but sometimes I still have to minimize and reopen the window. I don't want to have to do this, because it is going to be a commercial video game and people shouldn't have to do that.

If you need it, here is the code for my launcher window:

package net.renderedspoon.sombrero.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import net.renderedspoon.sombrero.Configuration;
import net.renderedspoon.sombrero.Game;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import net.renderedspoon.sombrero.RunGame;

public class Launcher extends JFrame {
private static final long serialVersionUID = 1L;

protected JPanel window = new JPanel();
private JButton play, options, help, exit;
private Rectangle rplay, roptions, rhelp, rexit;

Configuration config = new Configuration();

private int width = 250;
private int height = 350;
protected int button_width = 80;
protected int button_height = 40;

public Launcher(int id) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    setTitle("Launcher || Sombrero");
    setSize(new Dimension(width, height));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
    window.setLayout(null);
    requestFocus();
    if(id == 0) {
        drawButtons();
    }
}

private void drawButtons() {
    play = new JButton("Play");
    rplay = new Rectangle((width / 2) - (button_width / 2), 50, button_width, button_height);
    play.setBounds(rplay);
    window.add(play);

    options = new JButton("Options");
    roptions = new Rectangle((width / 2) - (button_width / 2), 100, button_width, button_height);
    options.setBounds(roptions);
    window.add(options);

    help = new JButton("Help");
    rhelp = new Rectangle((width / 2) - (button_width / 2), 150, button_width, button_height);
    help.setBounds(rhelp);
    window.add(help);

    exit = new JButton("Quit");
    rexit = new Rectangle((width / 2) - (button_width / 2), 200, button_width, button_height);
    exit.setBounds(rexit);
    window.add(exit);

    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: PLAY");
            dispose();
            new RunGame();
        }
    });

    options.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: OPTIONS");
            dispose();
            new Options();
        }
    });

    help.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: HELP");
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Game.debug) System.out.println("BUTTON HIT: EXIT");
            System.exit(0);
        }
    });
}


}

You're adding buttons after the call to

setVisible(true);

Call the method that draws your buttons before that line.

EDIT:

If you're adding buttons after the GUI is already visible, you might want to call validate() after adding the buttons. You can find more details on that here: http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#validate%28%29

I was also having this same issue, what I did was make the frame visible after I created all the panels & buttons.

ex)

frame.setVisible(true);
frame.add(panel);
panel.setVisible(true);
panel.add(button);
button.setVisible(true);

So, the key is to make the frame visible after the creation of all the panels and buttons.

Try that, maybe it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM