I'm trying to make minesweeper and a struggling trying to make a background image for my JFrame. I think the program breaks when setContentPane(screen) is run.
//pakke der styrer farver import java.awt.Color; import javax.swing.*; //Pakke der hjælper med at lave vinduet import javax.swing.JFrame; import java.awt.*; import java.awt.Font; import java.awt.FontFormatException; import java.io.File; import java.io.IOException; import java.awt.event.*; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.GraphicsEnvironment; import javax.imageio.ImageIO; import javax.swing.ImageIcon; public class GameFrame extends JFrame { // CENTER regions størrelse static final int SCREEN_WIDTH = 1100; static final int SCREEN_HEIGHT = 800; Font technology; // De forskellige JPanels JPanel topMenu; JPanel screen; JLabel mineCounter; JLabel timer; JButton btnNewButton; GameFrame() { try { setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Images/background.png"))))); } catch (IOException e) { e.printStackTrace(); } pack(); try { // load a custom font in your project folder technology = Font.createFont(Font.TRUETYPE_FONT, new File("technology.regular.ttf")).deriveFont(100f); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("technology.regular.ttf"))); } catch (IOException | FontFormatException e) { } screen = new JPanel(); screen.setLayout(new BorderLayout()); // adding top menu addTopMenu(); // adding game screen.add(new GamePanel(), BorderLayout.CENTER); // adding side menu //add(screen); setContentPane(screen); // Stopper programmet når vinduet lukkes setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Vinduets størrelse // setSize(SCREEN_WIDTH, SCREEN_HEIGHT); // Vinduets baggrundsfarve //this.setBackground(Color.BLACK); // Viser vinduet //setVisible(true); setVisible(true); // Gør at man ikke kan ændre størrelse på vinduet setResizable(false); } // Indeling af kode. Den del af koden som laver top menuen. void addTopMenu() { topMenu = new JPanel(); topMenu.setLayout(new BoxLayout(topMenu, BoxLayout.X_AXIS)); topMenu.setBackground(Color.gray); mineCounter = new JLabel(Mine.remaningMines + ""); mineCounter.setBackground(new Color(192, 192, 192)); mineCounter.setForeground(Color.red); mineCounter.setFont(technology); topMenu.add(mineCounter); topMenu.add(Box.createRigidArea(new Dimension(390, 0))); btnNewButton = new JButton(); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { GamePanel.newGame(); } }); String iconfilePath = this.getClass().getClassLoader().getResource("Images/Defualt.png").getFile(); btnNewButton.setIcon(new ImageIcon(iconfilePath)); btnNewButton.setBorder(BorderFactory.createEmptyBorder()); btnNewButton.setContentAreaFilled(false); btnNewButton.setFocusable(false); topMenu.add(btnNewButton); topMenu.add(Box.createRigidArea(new Dimension(390, 0))); timer = new JLabel(GamePanel.time + "00"); timer.setBackground(new Color(192, 192, 192)); timer.setForeground(Color.red); timer.setFont(technology); topMenu.add(timer); screen.add(topMenu, BorderLayout.NORTH); } public void changeIcon(String imagePath) { String iconfilePath = this.getClass().getClassLoader().getResource(imagePath).getFile(); btnNewButton.setIcon(new ImageIcon(iconfilePath)); } }