10 Replies - 2830 Views - Last Post: 21 August 2011 - 10:40 PM Rate Topic: -----

#1 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

JPanel not appearing in JFrame

Posted 21 August 2011 - 06:08 AM

Hi
i'm a beginner in java.i'm working on a project -puyo puyo game.
For this i first created a frame and searate jpanels in netbeans.What i want is when user clicks on "New" button (in MenuPanel)a second panel should open up.

here are the code i've written so far:

Base.java-its the JFrame which appears on start.
import java.awt.Component;
import java.awt.Container;

public class Base extends javax.swing.JFrame {

    Second sec;
    MenuPanel mp;
    /** Creates new form First */
    public Base() {
        initComponents();
        setBounds(300, 50, 330, 438);
        //Second sec;
        //sec = new Second(this);
        //Container c = getContentPane();
        
        //c.add(sec) ;
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 318, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Base().setVisible(true);
                new MenuPanel().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}



MenuPanel.java
it contains teh menuscreen
public class MenuPanel extends javax.swing.JPanel {

    /** Creates new form MenuPanel */
    public MenuPanel() {
        initComponents();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        start = new javax.swing.JButton();
        help = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        start.setText("NEW");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        help.setText("HELP");
        help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpActionPerformed(evt);
            }
        });

        exit.setText("EXIT");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(140, 140, 140)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(help, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(start, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                        .addGap(168, 168, 168))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(help, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(60, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
        new Second(new Base()).setVisible(true);
}                                     

    private void helpActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
}                                    

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                     
        System.exit(0);
}                                    

    // Variables declaration - do not modify                     
    javax.swing.JButton exit;
    javax.swing.JButton help;
    javax.swing.JButton start;
    // End of variables declaration                   
}


panel for game screen:
import java.awt.Image;
import java.util.Random;
import java.util.Timer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Second.java
 *
 * Created on Aug 16, 2011, 9:36:02 PM
 */
/**
 *
 * @author Administrator
 */
public class Second extends javax.swing.JPanel {

    int icons[][]=new int[12][6];//holds info about icons to display
    int x1,y1,x2,y2,c1,c2;       //holds moving poke info
    String s;								//Formation of chain combo is checked using this string
	Timer t1,t2,t3;							//different timers used for placing delays
	Image images[]=new Image[4];			//holds 4 pokemons
	Random randNumber;						//this object used to generate random numbers
	int rotation;							//used for ordering of icons
	boolean reached;						//it reached bottom or not
	int count;							//count of icons when formed a chain combo
	static int level;						//level of the game
	int score,erasedSmileys;
        MenuPanel mp;
	Base myBase;
    /** Creates new form Second */
    public Second(Base fs) {
        initComponents();
    }


    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}



My query is when i run this program i only see a blank JFrame but not my panel.Also if i add an object of second.java in start button of MenuPanel.java ,will i be able to see the gamescreen where i intend to add the gameplay elements?

This post has been edited by akira_: 21 August 2011 - 06:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JPanel not appearing in JFrame

#2 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 06:14 AM

edit:when i try this using
Container c = getContentPane();
c.add(mp) ;



i get a long list of exceptions starting with nullPointer exceptions.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 06:14 AM

new Second(new Base()).setVisible(true);


Here you create your panel and make it visible, but you also need to add it to either your frame or a new frame (not sure which you want). If you add it to your existing frame, you might need to call the revalidate() method on the JFrame.
Was This Post Helpful? 0
  • +
  • -

#4 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 06:20 AM

um i want to add the MenuPanel in JFrame not the game screen.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 06:26 AM

Did I get the wrong bit of code? Maybe you could post the part you need help with so I don't have to keep guessing.
Was This Post Helpful? 0
  • +
  • -

#6 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 06:46 AM

i just want the MenuPanel.java(with its three buttons) panel to appear on a JFrame(Base.java) when i run the program but instead i'm getting only a blank screen.
new Base().setVisible(true);

new MenuPanel().setVisible(true);


i want both of them to appear when i run my project
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 07:35 AM

I can't see where you added the menu panel to the base frame.

Maybe this:

Base window = new Base();
window.add(new MenuPanel());
window.pack();
window.setVisible(true);

Was This Post Helpful? 0
  • +
  • -

#8 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 07:48 AM

Hi ,
When i add this in main method i still get only a blank jframe in the output. :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 08:08 AM

What happens when you change it to this:

JFrame window = new JFrame();
window.add(new MenuPanel());
window.pack();
window.setVisible(true);

Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 02:30 PM

new Second(new Base()).setVisible(true);

and what do you do with that new Second object ? You just created it but do nothing with it
You would have

add( new Second(new Base()).setVisible(true));

would have make more sense
Was This Post Helpful? 0
  • +
  • -

#11 akira_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 11

Re: JPanel not appearing in JFrame

Posted 21 August 2011 - 10:40 PM

View Postcfoley, on 21 August 2011 - 08:08 AM, said:

What happens when you change it to this:

JFrame window = new JFrame();
window.add(new MenuPanel());
window.pack();
window.setVisible(true);


yep it worked it worked :) ty
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1