Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Basically, I have my Frame class, which instantiates all the properties for the JFrame, and draws a JLabel with an image (my title screen). Then I made a separate JPanel with a start button on it, and made a mouse listener that will allow me to remove these objects while adding in a new Board() class (Which paints the main game).

*Note: The JLabel is SEPARATE from the JPanel, but it still gets moved to the side by it.

Problem: Whenever I click the button though, it only shows a little square of what I presume is my board class trying to run. Code below for the Frame Class:

package OurPackage;
//Made By A.K. 5/24/12
//Contains Frame.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;

public class Frame implements MouseListener
    public static boolean StartGame = false;
    ImageIcon img = new       ImageIcon(getClass().getResource("/Images/ActionJackTitle.png"));
    ImageIcon StartImg = new ImageIcon(getClass().getResource("/Images/JackStart.png"));
    public Image Title;
    JLabel TitleL = new JLabel(img);
    public JPanel panel = new JPanel();
    JButton StartB = new JButton(StartImg);
    JFrame frm = new JFrame("Action-Packed Jack");

    public Frame()
        TitleL.setPreferredSize(new Dimension(1200, 420));

        frm.setLayout(new GridBagLayout());

        panel.setSize(new Dimension(220, 45));
        panel.setLayout(new GridBagLayout ());

        StartB.setPreferredSize(new Dimension(220, 45));

        frm.setSize(1200, 420);

    public static void main(String[] args)
        new Frame();

    public void mouseClicked(MouseEvent e) 
            frm.add(new Board()); //Add Game "Tiles" Or Content. x = 1200

    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub


    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub


    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub


    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub



EDIT: I also created a new Board Object(?) in my Frame class. Will I be able to resize it and such?

share|improve this question
up vote 0 down vote accepted

First of all, ALL component manipulations should run in the Event Dispatch Thread (EDT) or else it WILL cause you trouble. In your case:

public static void main(String[] args)
    SwingUtilities.invokeLater(new Runnable() 
       public void run() 
          new Frame();

As for your problem, i guess it is a layout issue. I suggest you read these tutorials.

< personal-opinion > I made a reasonable amount of swing apps, and from my experience, the only layout managers worth using are GridLayout and BorderLayout. < /personal-opinion >

share|improve this answer
I'll keep you updated, since by adding repaint(); and revalidate(); methods I have already been able to clear the screen for the new game board to begin. I just need to figure out some sort of formatting since it is still a small square in the middle of the window even when removing the JPanel. – A13X Jun 5 '12 at 22:13
The new Board() object that is, only shows up as a small square. And yes, I removed everything pretty much possible except the JFrame itself, so it should at least show in the full window... – A13X Jun 6 '12 at 4:45
I guess it was a CardLayout I needed. HOWEVER-There is one minor issue: My character won't move, which is what I'm assuming to be a problem with the KeyEvents in the Player Class. Thanks for the help though! – A13X Jun 6 '12 at 23:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.