0

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;
        } 
    }        
}
Community
  • 1
  • 1
  • 1
    Why does OgGui extend JPanel? That class just contains the main() method, there is no need for it to extend anything. Don't use a while loop to display the frame. Your question is about a frame, an image and a button, so post a proper [mcve] that demonstrates the problem. You code should only include those components, since they are the components relevant to the question. – camickr May 31 '17 at 17:05
  • `while (start==true) {` Why is that code being done in a loop, let alone (what seems like) an endless loop? – Andrew Thompson May 31 '17 at 23:04
  • *"Does anyone know how to fill the frame window with the image?"* In the first instance, I'd remove `frame.setSize(400, 600);` and uncomment `//frame.pack();`. Note that the GUI should be set visible after all components are added and the components laid out by being packed. It might be possible to provide further (attention to this problem and) tips once the MCVE suggested by @camickr has been posted as an [edit]. `ImageIcon icon = new ImageIcon("og.png");` 1) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson May 31 '17 at 23:12
  • .. 2) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson May 31 '17 at 23:14

0 Answers0