0

I was wondering why my button does not show up on the panel until AFTER I hover my mouse over where it would be? It also disappears again if I resize the window. The MainMenuScreen is just an image I use as my background image.

    //MainMenu setup
    JPanel card2 = new JPanel();
    card2.setLayout(new GridBagLayout());       
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(2,2,2,2);
    gbc.anchor = GridBagConstraints.CENTER;
    MainMenuScreen mms = new MainMenuScreen();
    mms.setLayout(new FlowLayout());
    card2.add(mms);
    card2.add(menuButton1, gbc);

Here is how I setup by background image.

public class MainMenuScreen extends JPanel{
    private static final long serialVersionUID = 1L;

    private BufferedImage background;

    public MainMenuScreen() {
        try {
            background = ImageIO.read(new File("M&M Arcade.png"));          
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (background != null) {
            int x = (getWidth() - background.getWidth());
            int y = (getHeight() - background.getHeight());
            g.drawImage(background, x, y, this);
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(Color.white);
    }
}
msteppe91
  • 143
  • 2
  • 18

1 Answers1

1

The JButton doesn't show up beacuse you are using the same GridBagConstraints values for your MainMenuScreen component and your JButton menuButton1, i.e. they exist at the same location. Once the values of gbc are adjusted, the button will appear. Also better to consistently use the correct overloaded method of add when adding to a container with GridBagLayout.

Edit:

There have been numerous discussions on how to implement background images on JPanel containers. A notable one is Background Panel.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I would like the button to show up on top of the image. Is there a way I can do that? @Reimeus – msteppe91 Mar 16 '13 at 18:09
  • Sure, there are loads of posts on how that can be done such as [here](http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel). Also see update – Reimeus Mar 16 '13 at 18:31
  • I actually do one of those @Reimeus See edit. I feel like the image is being treated as a component in the gridbaglayout? – msteppe91 Mar 16 '13 at 18:42
  • Your `menuButton1` would need to be added to your `MainMenuScreen` for the image to appear behind the button. – Reimeus Mar 16 '13 at 18:45
  • Instead of card2.add(menuButton1, gbc); I use mms.add(menuButton1, gbc); How then can I position my button to be where I need it? @Reimeus – msteppe91 Mar 16 '13 at 19:07
  • That question deserves a new post. In the new post add a description of your positioning requirements and add an [SSCCE](http://sscce.org/) :) – Reimeus Mar 16 '13 at 19:10
  • posted "JButton positioning on a JPanel using GBC/GBL" – msteppe91 Mar 16 '13 at 19:35