Results 1 to 3 of 3
Thread: JPanel not showing up in JFrame
- 06-25-2010, 06:40 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
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:
And here is my main program area: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); } } }
Why is the panel not being displayed? Is my question here.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); } }
- 06-25-2010, 07:13 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
> 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)
- 06-25-2010, 07:21 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
Showing JList in a JPanel
By nico.hvi in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 02:26 PM -
JPanel/JFrame
By Mayur in forum New To JavaReplies: 2Last Post: 12-20-2009, 05:07 AM -
JFrame created but window contents not showing, othr threads keep waiting on the user
By FezKazi in forum AWT / SwingReplies: 1Last Post: 02-20-2009, 03:49 PM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks