0

I'm trying to make a GUI for my almost finished Java game. When I run the code there are two different windows, one for the JFrame and I think the other one for the JLabel or so but I want that there is only one window displaying the png as a background and that I can place buttons etc over it.

Right now the label displays the png and the panel/frame just the button. Is there a solution for it?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.sound.sampled.*;

import javax.sound.sampled.spi.AudioFileReader;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

class GUI extends JFrame implements ActionListener {;
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JLabel label = new JLabel();

    public GUI(){
        playMusic();
        setTitle("start-menu");
        setSize(700,700);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout());

        setContentPane(new JLabel(new ImageIcon("filepath")));
        setLayout(new FlowLayout());
        add(label);
        setSize(800,800);

        JButton button = new JButton("PLAY");
        button.setVisible(true);
        button.setBorderPainted(false);

        panel.setBorder(BorderFactory.createEmptyBorder(400,400,400,400));
        panel.setLayout(new GridLayout(0,1));
        panel.add(label);
        panel.add(button);

        frame.add(panel,BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("GUI");
        frame.pack();
        frame.setVisible(true);
    }

    public static void playMusic(){
        Clip clip;
        try {
            AudioInputStream input=AudioSystem.getAudioInputStream(new File("music filepath"));
            clip=AudioSystem.getClip();
            clip.open(input);
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    You seem to want to use the image as a background for the panel in which the button(s) appear. The usual way to achieve that is to use custom painting to draw the image, then adding the buttons to that panel. – Andrew Thompson Oct 15 '21 at 21:58
  • do you have an example? would be very nice – Kramerlle Oct 15 '21 at 22:03
  • 1
    Take a look at the first answer to this [Stack Overflow question](https://stackoverflow.com/questions/16058736/drawing-a-background-image). That should be enough to get you started. – Gilbert Le Blanc Oct 16 '21 at 01:30
  • 1
    If you want the image to scale as the frame size changes you can do custom painting of the image on a panel. If you want the image always displayed at its actual size then you can use a JLabel. See: [Background Panel](https://tips4java.wordpress.com/2008/10/12/background-panel/) for more information and examples. – camickr Oct 16 '21 at 01:58
  • Thanks @GilbertLeBlanc! I had a quick look for examples but got distracted by other things. – Andrew Thompson Oct 16 '21 at 02:11
  • 1
    @Andrew Thompson: You're welcome. I usually use my search engine (DDG) to search Stack Overflow, so I usually find what I'm looking for. – Gilbert Le Blanc Oct 16 '21 at 02:16

0 Answers0