Results 1 to 2 of 2
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Loop issues - Frame not showing up

    I had an idea to add a timer to my java hangman game to use as a scoring system instead. So, I wanted to implement a difficultyPanel which would loop until the diffSet boolean gets set to true. In which case, the difficultyPanel could then be removed and the game could ensue. Well, I have a problem. My frame isnt being shown if I get the loop to work. I don't understand what the problem is. The expected result was to have the setDifficulty() loop until I broke out of it by setting the diffSet to true via a button press. Can anyone help point me in a direction where I have maybe missed something.

    Java Code:
    package com.uh.ryan;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class Starter {
    
    	private JFrame frame;
    	private Dimension d = new Dimension(800,600);
    	
    	public Starter(){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				frame = new JFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setPreferredSize(d);
    				frame.setMinimumSize(d);
    				frame.setMaximumSize(d);
    				frame.setResizable(false);
    				frame.setLocationRelativeTo(null);
    				
    			Game game = new Game();
    			frame.add(game);
    			frame.pack();
    			frame.setVisible(true);
    			}
    		});	
    	}			
    	
    	public static void main(String[] args){
    		new Starter();
    	}
    }
    Java Code:
    package com.uh.ryan;
    
    import java.awt.Color;
    import java.awt.Container;
    
    import javax.swing.JPanel;
    
    public class Game extends Container{
    
    
    	private JPanel gamePanel, diffPanel;
    	private boolean diffSet = false;
     	
    	public Game(){
    		init();
    		setDifficulty();
    	}
    	
    	public void init(){
    		
    		gamePanel = new JPanel();
    		gamePanel.setLayout(null);		
    		gamePanel.setSize(800, 600);
    		gamePanel.setLocation(0, 0);
    		gamePanel.setVisible(true);
    		add(gamePanel);
    		
    		diffPanel = new JPanel();
    		diffPanel.setSize(800, 600);
    		diffPanel.setLocation(0, 0);
    		diffPanel.setVisible(false);
    		gamePanel.add(diffPanel);
    	}
    	
    	public void setDifficulty(){
    		while(diffSet == false){
    			diffPanel.setVisible(true);
    			System.out.println("DiffPanel Vis = " + diffPanel.isVisible());
    			System.out.println("diffSet = " + diffSet);
    			}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Loop issues - Frame not showing up

    It looks like your invoking the game() while in the EDT. Game contains an endless loop which ties up the EDT so painting can't take place.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Showing multi frames within a single main frame
    By chandrupsekar in forum AWT / Swing
    Replies: 6
    Last Post: 11-24-2011, 03:55 PM
  2. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  4. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 04:24 AM
  5. Issues with a while loop
    By Lang in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •