I have a JPanel
that holds the main menu screen for a game project in my computer science class. The MainMenu
class creates a panel that holds a button to start the game as well as a background image. However, when I add the panel to the parent JFrame
(defined in OgGui
class), there is about a cm of space between the top of the frame and where the panel/background image starts.
Does anyone know how to fill the frame window with the image?
//Creates parent JFrame to hold all game components
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OgGui extends JPanel{
public static void main(String[] args){
JFrame frame = new JFrame ("OG Ball");
boolean start=true;
MainMenu mainm=new MainMenu();
while (start==true) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setVisible(true);
frame.add(mainm);
start=mainm.returnscreen();
}
frame.add(new Draw());
//frame.pack();
}
}
MainMenu
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
public class MainMenu extends JPanel{
//Creates JFrame and menu background
JPanel main = new JPanel();
ImageIcon icon = new ImageIcon("og.png");
JLabel menu = new JLabel(icon);
//Sets up button to begin play
JButton startBtn = new JButton();
boolean gamestart;
public MainMenu(){
startBtn.setSize(4, 4);
startBtn.setVisible(false);
startBtn.addActionListener(new startScreen());
main.add(menu);
main.add(startBtn);
main.revalidate();
this.add(main);
}
public boolean returnscreen() {
return gamestart; }
private class startScreen implements ActionListener{
public void actionPerformed(ActionEvent event){
gamestart=false;
}
}
}