Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JPanels appearing when they shouldn't

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanels appearing when they shouldn't

    hi, I'm creating a game. I made a MenuPanel Jpanel and a GamePanel Jpanel to use in my JFrame. I set the MenuPanel to create itself first and appear first, forcing the GamePanel to not be created until a boolean value is passed. However, when I run the program the GamePanel draws itself and begins it's animations. Here is the snippet of code in which the panels are created.

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    MenuPanel menu = new MenuPanel();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(600,500);
    frame.setTitle("Breakout");
    frame.setResizable(false);
    frame.add(menu);
    frame.setVisible(true);
    if(menu.startGame = true){
    GamePanel game = new GamePanel();
    frame.add(game);
    game.start();
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanels appearing when they shouldn't

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    What's the question?

    Create the app's primary container in the container's constructor, not in the main() method. The main() method should call the primary container's constructor on the EDT. Beyond that, you haven't given enough detail to understand your question or determine an answer.

    --- Update ---

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    What's the question?

    Create the app's primary container in the container's constructor, not in the main() method. The main() method should call the primary container's constructor on the EDT. Beyond that, you haven't given enough detail to understand your question or determine an answer.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanels appearing when they shouldn't

    sorry about the code. as for the container. I'm just using JFrame as my container (I believe that's what your trying to tell me anyways [I'm WAY new at this]) and the way I set up the main method was so it would create the JFrame, create my MenuPanel, and put the MenuPanel on the JFrame. In the MenuPanel I created a method that, upon hearing the space bar, sets a boolean value initiated as false to true. The if statement in my Main method is what asks the MenuPanel what the value is, and if true, enters the if code. The thing is, I put all of the GamePanel in the if statement, so Java shouldn't even know what a GamePanel is until it gets into the if statement, but when I run the program it runs the GamePanel (and thus the game). I didn't think this is how it was supposed to work but that's the way it is. Also, maybe the fact that MenuPanel and GamePanel are nested classes in the Breakout class (my game's name/class) are what is causing this? I'm just wondering why it would create the GamePanel before it gets into the if statement.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanels appearing when they shouldn't

    Then you should think to yourself, "MongMorg, there must be something wrong with the if statement," inspect it, and fix it. The following statement contains one of the most common (maybe the two most common) beginner errors:

    if(menu.startGame = true)

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanels appearing when they shouldn't

    I SEE IT. CURSE YOU DOUBLE EQUALS.

    --- Update ---

    I was able to fix everything. that equals sign real screwed everything up. thanks!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanels appearing when they shouldn't

    Mildly ironic, because the comparison or equality operator wasn't required in the if condition. A boolean can be the condition.

    It might help to avoid these fundamental errors by using proper terminology and therefore associating the symbols with what they do. '==' and '!=' are equality operators. '=' is one of many assignment operators. There is no 'double equals' operator.

    In case I wasn't clear in my first post, most of the code in your main() method belongs somewhere else, either in a constructor or in another method in a driver class. The main() method should be used to start the Swing app on the EDT. The resulting main() method should look something like:
    public static void main( String[] args )
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                new DriverClass();
     
            } // end method run()
     
        } ); // end method invokeLater()
     
    } //end method main()

Similar Threads

  1. [SOLVED] This shouldn't be too hard.
    By MR bruto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2013, 11:36 AM
  2. menu bar is not appearing
    By saurabh_kabra in forum AWT / Java Swing
    Replies: 8
    Last Post: March 29th, 2013, 01:52 PM
  3. Components not appearing (need help!)
    By hawkeye01 in forum Object Oriented Programming
    Replies: 6
    Last Post: March 24th, 2013, 09:41 AM
  4. [SOLVED] Confused about string that I think shouldn't be null
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2011, 11:08 PM

Tags for this Thread