Member Avatar for Member #886280

Inline Code Example Here

class MainMenu extends JPanel {

public MainMenu() {

    this.setLayout(null);

    JButton newGame = new JButton("New Game");
    JButton loadGame = new JButton("Load Game");
    JButton scoreGame = new JButton("High Score");
    JButton exitGame = new JButton("Exit Game");

    this.add(newGame);
    newGame.setBackground(Color.white);
    newGame.setPreferredSize(new Dimension(180, 50));
    newGame.setFont(new Font("Fixedsys", 1, 25));

    this.add(loadGame);
    loadGame.setBackground(Color.white);
    loadGame.setPreferredSize(new Dimension(180, 50));
    loadGame.setFont(new Font("Fixedsys", 1, 25));



    this.add(scoreGame);
    scoreGame.setBackground(Color.white);
    scoreGame.setPreferredSize(new Dimension(180, 50));
    scoreGame.setFont(new Font("Fixedsys", 1, 25));




    this.add(exitGame);
    exitGame.setBackground(Color.white);
    exitGame.setPreferredSize(new Dimension(180, 50));
    exitGame.setFont(new Font("Fixedsys", 1, 25));


    Insets insets = this.getInsets();

    Dimension size = newGame.getPreferredSize();
    newGame.setBounds(450 + insets.right, 80 + insets.top,
            size.width, size.height);

    size = loadGame.getPreferredSize();
    loadGame.setBounds(450 + insets.left, 130 + insets.top,
            size.width, size.height);


    size = scoreGame.getPreferredSize();
    scoreGame.setBounds(450 + insets.left, 180 + insets.top,
            size.width, size.height);

    size = exitGame.getPreferredSize();
    exitGame.setBounds(450 + insets.left, 230 + insets.top,
            size.width, size.height);

}

public void paint(Graphics g) {
    Image img1 = Toolkit.getDefaultToolkit().getImage("snake.jpg");
    g.drawImage(img1, 0, 0, this);
}

public static void main(String[] args) {
    MainMenu game = new MainMenu();
    JFrame frame = new JFrame();
    frame.setSize(655, 338);
    frame.add(game);
    frame.setVisible(true);
    frame.setTitle("Snake");
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}

I´m new at java. And i want to know how to put Jbuttons in JPanel in mainMenu. When i compilate this gui i see image but the buttons are hidden behind image.

Dani AI

Generated

Your buttons are being obscured because the panel is painting the image the wrong way and the image is being loaded inside the paint method. As hinted, Swing custom painting belongs in paintComponent, not paint. paintComponent is the place to draw a background image (and call super.paintComponent(g) first). Do not load the image inside the paint method — load it once (constructor or initialization) so painting is fast and deterministic.

Use a synchronous loader (for example ImageIO.read(...) or new ImageIcon(...).getImage()) instead of Toolkit.getDefaultToolkit().getImage(...), which is asynchronous and can produce flicker or incomplete drawing. Keep the image as a BufferedImage field and draw it inside paintComponent.

If you want the buttons to sit on top of the image, prefer a layout manager (Vertical BoxLayout, GridBagLayout, etc.) or one of the overlay options that mentioned (OverlayLayout or JLayeredPane). If you stack a transparent panel of buttons over a background panel, call buttonPanel.setOpaque(false) so the background shows through.

Small example outline:

private BufferedImage bg;

public MainMenu() {
  bg = ImageIO.read(getClass().getResource("/snake.jpg")); // load once
  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  // add buttons here
}

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  if (bg != null) g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
}

Other practical tips: build the UI on the Event Dispatch Thread (SwingUtilities.invokeLater), avoid null layouts unless necessary, call frame.pack() when using preferred sizes, and use revalidate() / repaint() after dynamic changes. If the image still behaves oddly, check resource path (getResource can return null) and test with an ImageIcon to verify the file is found.

Recommended Answers

All 4 Replies

Its tricky to get the buttons to be drawn after you draw the image. One thing you should override the paintComponent method for swing classes.

other than that you could use an overlayLayout
1) create a JPanel and set its layout to OverlayLayout
2) create a new JPanel and add the image to this then add this JPanel to the JPanel created in 1
3) create another JPanel and add your buttons here then add this JPanel to the JPanel created in 1
4) add the JPanel created in 1 to the JFrame.

Member Avatar for Member #886280

quickly example of that JPanels...zeroliken? please :)

here's an example but with just buttons:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.