0
\$\begingroup\$

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.add(TitleL);

        frm.setLayout(new GridBagLayout());
        frm.add(panel);

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

        panel.add(StartB);
        StartB.addMouseListener(this);
        StartB.setPreferredSize(new Dimension(220, 45));

        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setSize(1200, 420);
        frm.setVisible(true);
        frm.setResizable(false);
        frm.setLocationRelativeTo(null);
    }

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

    public void mouseClicked(MouseEvent e) 
    {
            StartB.setContentAreaFilled(false);
            panel.remove(StartB);
            frm.remove(panel);
            frm.remove(TitleL);
            //frm.setLayout(null);
            frm.add(new Board()); //Add Game "Tiles" Or Content. x = 1200
            frm.validate();
            System.out.println("Hit!");
    }

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

    }

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

    }

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

    }

    @Override
    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?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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 >

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – A13X
    Jun 5, 2012 at 22:13
  • \$\begingroup\$ 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... \$\endgroup\$
    – A13X
    Jun 6, 2012 at 4:45
  • \$\begingroup\$ 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! \$\endgroup\$
    – A13X
    Jun 6, 2012 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .