Can't remove JPanel from JFrame while adding new class into it
- by A.K.
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
}
}