Results 1 to 3 of 3
  1. #1
    rlindsey is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default JPanel not showing up in JFrame

    This project is suppose to display a group of boxes 100 pixels square from 1 to 5 that is entered by the user. If the user enters 1 then a single box 100 wide and 100 high is supposed to be shown, if they enter 2 then a stack of boxes the same height and width each 2X2 is supposed to be shown. There is a class to make the panel and the drawlines and the main program area that calls up the class, with a set size passed, and then adds it to the JFrame and displays it. The JFrame is supposed to be 10 pixels wider around then the panel. The JFrame operates correctly, shrinking and expanding as it is needed, but the JPanel graphics do not display ever.

    Here is my class code:
    Java Code:
    import java.awt.*; 
    import java.awt.Graphics; //imports the java graphics
    import javax.swing.JPanel; //imports the java panel
    
    public class MakeBox extends JPanel
    {
    	//class variable
    	private int sizer = 110; //integer sizer for box size with default set
    	
    	//constructor that takes the size of the box
    	public MakeBox(int newSize)
    	{
    		sizer = newSize;
    	}	
    	
    	//public method that takes one integer value to construct and display the box
    	public void CreateBox(Graphics g)
    	{
    		//method variables
    		int i = 110; //sets up a loop value at default value
    		
    		//call paintComponent for correct display
    		super.paintComponent(g);		
    		
    		//set graphic color
    		g.setColor(Color.BLACK);
    				
    		//draw the outer box lines
    		g.drawLine( 10, 10, sizer, 10);
    		g.drawLine( 10, 10, 10, sizer);
    		g.drawLine( sizer, 10, sizer, sizer);
    		g.drawLine( 10, sizer, sizer, sizer);
    		
    		//loop to draw intersecting lines
    		for( i = 110; i < sizer; i = i + 100)
    		{
    			//draw vertical line
    			g.drawLine( i, 10, i, sizer);
    			
    			//draw horizontal line
    			g.drawLine (10, i, sizer, i);
    		}
    	}
    }
    And here is my main program area:

    Java Code:
    import javax.swing.JOptionPane; //import the java pane
    import javax.swing.JFrame; //imports the java frame
    import java.awt.Graphics;
    import java.awt.*;
    
    public class BoxProgram
    {	
    	public static void main( String[] args)
    	{
    		//variables
    		boolean tester = false; //boolean to test for valid entry
    		String getNumber = "1"; //string for user entry set at default value
    		int thisNumber = 0; //integer for string conversion
    		int drawBox = 110; //integer for height and width of box set at default value		
    		int boxSize = 120; //integer for the size of the box set at default value
    				
    		//test do while loop
    		do
    		{
    			//get user entry
    			getNumber = JOptionPane.showInputDialog(" Please enter a number from 1 to 5:");
    			
    			//try catch to test for valid entry
    			try
    			{
    				//try to convert entry into integer value
    				thisNumber = Integer.parseInt( getNumber );
    				
    				//if statement to check for values between 1 and 5
    				if (thisNumber > 0 && thisNumber < 6)
    				{
    					//set box size
    					boxSize = thisNumber * 100 + 20;
    					
    					//set the width and height of the box
    					drawBox = boxSize - 10;					
    					
    					//create new box class panel
    					MakeBox thisBox = new MakeBox(drawBox);				
    										
    					//create a frame to hold the panel
    					JFrame thisFrame = new JFrame();
    					
    					//set frame to exit when closed
    					thisFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    					
    					//add panel to the frame
    					thisFrame.add( thisBox );
    					
    					//set frame size
    					thisFrame.setSize ( boxSize, boxSize );					
    					
    					//make frame visible
    					thisFrame.setVisible( true );			
    					
    					//set boolean value
    					tester = true;
    				}
    				else
    				{
    					//display out of range error message
    					JOptionPane.showMessageDialog(null, "Please enter a number from 1 to 5.");
    					
    					//set error boolean
    					tester = false;
    				} 
    			}
    			catch(Throwable t)
    			{
    				//display invalid entry type error message
    				JOptionPane.showMessageDialog(null, "Please enter a valid numeric value.");
    				
    				//set error boolean
    				tester = false;
    			}
    		}		
    		while (tester == false);
    	}
    }
    Why is the panel not being displayed? Is my question here.

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > Why is the panel not being displayed? Is my question here.

    this might be a good place to start

    //public void CreateBox(Graphics g)
    public void paintComponent(Graphics g)

  3. #3
    rlindsey is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you, it's working now. I was going by the example code from the book and it had it set up the first way, but your way works and the first way did not. lol

Similar Threads

  1. Replies: 6
    Last Post: 06-16-2010, 06:03 AM
  2. Showing JList in a JPanel
    By nico.hvi in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 02:26 PM
  3. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 AM
  4. Replies: 1
    Last Post: 02-20-2009, 03:49 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 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
  •