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) {
}
}