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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 72

Thread: Mutiple classfile, JPanel nothing shows up

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Mutiple classfile, JPanel nothing shows up

    I had 2 .java files and I decided it was too much stuff in two file .java so I made more class but now nothing is showing up :\


    myFrame.java
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class myFrame extends JPanel implements ActionListener,KeyListener {
    	private static final long serialVersionUID = 1L;
     
    	Timer t = new Timer(5, this);
     
    	public myFrame() {
    		t.start();
    		addKeyListener(this);
    		setVisible(true);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);		
    	}
     
     
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}	
    }

    arkanoidGame.java

    import javax.swing.JFrame;
     
    public class arkanoidGame {
     
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		myFrame s = new myFrame();
    		Ball b = new Ball();
    		pad p = new pad();
    		frame.add(p);
    		frame.add(b);
    		frame.add(s);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,600);
    		frame.setVisible(true);
    	}	
    }

    ball.java
    import javax.swing.*;
    import java.awt.*;
     
     
    public class Ball extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	int ballX = 60;
    	int ballY = 60;
    	int ballWH = 25;
     
     
    	public void init() { 
    		setBackground(Color.BLUE);
    	}
     
    	public void start() {
    		// define a new thread 
    		Thread th = new Thread (this); 
    		// start this thread 
    		th.start (); 
    		} 
     
     
    	public void stop() { } 
     
    	public void destroy() { } 
     
     
     
    	public void paint (Graphics g) {
    		super.paint(g);
    		g.setColor(Color.blue);
    		g.fillOval(ballX-ballWH,ballY-ballWH,2*ballWH,2*ballWH);
    	} 
     
     
     
    	public void run() {
    		// lower ThreadPriority 
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
     
    		// run a long while (true) this means in our case "always" 
    		while (true) 
    		{ 
    			ballX++;
    			ballY++;
    			repaint(); 
     
    		try 
    		{
    		// Stop thread for 20 milliseconds 
    		Thread.sleep (20); 
    		} 
    		catch (InterruptedException ex) 
    		{
    		// do nothing 
    		} 
     
    		// set ThreadPriority to maximum value 
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
     
    		}
     
    	}
    }

    pad.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class pad extends JPanel implements KeyListener, ActionListener {
     
    	Timer t = new Timer(5, this);
     
    	double rectX = 15;
    	double rectY = 540;
    	int rectWidth = 80;
    	int rectHeight = 20;
    	double rectVeloX = 0.0;
    	double rectVeloY = 0.0;
     
    	private static final long serialVersionUID = 1L;
     
    	public void paint(Graphics draw2D){		
     
    		super.paint(draw2D);
    		Graphics2D draw = (Graphics2D) draw2D;
    		draw.setColor(Color.red);
    		draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
    		draw.getClipBounds(); 
     
    	}
     
    	public void actionPerformed (ActionEvent e) {
    	   	repaint();
    		rectX += rectVeloX;
    		rectY += rectVeloY;		
    	}
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
     
    		   if (key == KeyEvent.VK_RIGHT ) {
    			   if ((rectX+rectWidth) >= 800)
    			   {
    				   rectVeloX = 0;
    				   rectX = 800-rectWidth;
    			   }
    			   else {
    			   rectVeloX = 1.5;
    			   rectVeloY = 0;
    			   }
     
    	    }  if (key == KeyEvent.VK_LEFT ) {
    	    		if (rectX <= 0) 
    	    		{
    	    			rectVeloX = 0;
    	    			rectX = 0;
    	    		}
    	    		else {
    				   rectVeloX = -1.5;
    				   rectVeloY = 0;
    	    		}
    	    }
     
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}
    	}
     
    	public void keyTyped(KeyEvent e) {}		
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Lorack View Post
    I had 2 .java files and I decided it was too much stuff in two file .java so I made more class but now nothing is showing up :\


    myFrame.java
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class myFrame extends JPanel implements ActionListener,KeyListener {
    	private static final long serialVersionUID = 1L;
     
    	Timer t = new Timer(5, this);
     
    	public myFrame() {
    		t.start();
    		addKeyListener(this);
    		setVisible(true);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);		
    	}
     
     
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}	
    }

    arkanoidGame.java

    import javax.swing.JFrame;
     
    public class arkanoidGame {
     
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		myFrame s = new myFrame();
    		Ball b = new Ball();
    		pad p = new pad();
    		frame.add(p);
    		frame.add(b);
    		frame.add(s);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,600);
    		frame.setVisible(true);
    	}	
    }

    ball.java
    import javax.swing.*;
    import java.awt.*;
     
     
    public class Ball extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	int ballX = 60;
    	int ballY = 60;
    	int ballWH = 25;
     
     
    	public void init() { 
    		setBackground(Color.BLUE);
    	}
     
    	public void start() {
    		// define a new thread 
    		Thread th = new Thread (this); 
    		// start this thread 
    		th.start (); 
    		} 
     
     
    	public void stop() { } 
     
    	public void destroy() { } 
     
     
     
    	public void paint (Graphics g) {
    		super.paint(g);
    		g.setColor(Color.blue);
    		g.fillOval(ballX-ballWH,ballY-ballWH,2*ballWH,2*ballWH);
    	} 
     
     
     
    	public void run() {
    		// lower ThreadPriority 
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
     
    		// run a long while (true) this means in our case "always" 
    		while (true) 
    		{ 
    			ballX++;
    			ballY++;
    			repaint(); 
     
    		try 
    		{
    		// Stop thread for 20 milliseconds 
    		Thread.sleep (20); 
    		} 
    		catch (InterruptedException ex) 
    		{
    		// do nothing 
    		} 
     
    		// set ThreadPriority to maximum value 
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
     
    		}
     
    	}
    }

    pad.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class pad extends JPanel implements KeyListener, ActionListener {
     
    	Timer t = new Timer(5, this);
     
    	double rectX = 15;
    	double rectY = 540;
    	int rectWidth = 80;
    	int rectHeight = 20;
    	double rectVeloX = 0.0;
    	double rectVeloY = 0.0;
     
    	private static final long serialVersionUID = 1L;
     
    	public void paint(Graphics draw2D){		
     
    		super.paint(draw2D);
    		Graphics2D draw = (Graphics2D) draw2D;
    		draw.setColor(Color.red);
    		draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
    		draw.getClipBounds(); 
     
    	}
     
    	public void actionPerformed (ActionEvent e) {
    	   	repaint();
    		rectX += rectVeloX;
    		rectY += rectVeloY;		
    	}
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
     
    		   if (key == KeyEvent.VK_RIGHT ) {
    			   if ((rectX+rectWidth) >= 800)
    			   {
    				   rectVeloX = 0;
    				   rectX = 800-rectWidth;
    			   }
    			   else {
    			   rectVeloX = 1.5;
    			   rectVeloY = 0;
    			   }
     
    	    }  if (key == KeyEvent.VK_LEFT ) {
    	    		if (rectX <= 0) 
    	    		{
    	    			rectVeloX = 0;
    	    			rectX = 0;
    	    		}
    	    		else {
    				   rectVeloX = -1.5;
    				   rectVeloY = 0;
    	    		}
    	    }
     
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}
    	}
     
    	public void keyTyped(KeyEvent e) {}		
     
    }
    Can you explain how you are executing the program? Are there any error messages?
    What happens when you start the program?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    No errors... The windows opens but nothing in it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Check that you are using the layout manager correctly so that all the components added to a container are being positioned where you want them.

    Try doing some debugging by adding a drawLine() method call to the paint() method that will be sure to be seen when it executes. Also print out the bounds of the component when the paint method is called.

    BTW You should NOT override the paint() method for Swing classes. Better to override the paintComponent() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    BTW You should NOT override the paint() method for Swing classes. Better to override the paintComponent() method.
    If i use paintComponent() The repaint() method doesn't work.

    The rectangle leaves footprints in the Frame
    Last edited by Lorack; June 24th, 2012 at 04:53 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Use paintCompoment not PaintComponent
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Use paintCompoment not PaintComponent
    Sorry it was a typo... I used paintComponent and the rectangle let a footprints on his side when I moved it around

    I tryed the drawLine method and nothing appears on the screen.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    nothing appears on the screen.
    What bounds values were printed from printlns added to the paint()methods?

    Did you check the layout manager usage?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    I don't use a layout manager. I guess that my problem... in my arkanoidGame.java I do :

    JFrame frame = new JFrame();

    frame.setBounds(0,0,800,600); does it set the bounds?

    Nothing prints out

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    There is a default layout manager for containers.

    If nothing prints out then the paint() methods are not being called.

    If you print the bounds of the components from their paint() methods then you can see how big they are. In some cases the width and height can be so small that you don't see them on the screen.

    What is the MyFrame class used for? It does not have an override for the paint() method but it extends JPanel so I assume it should be showing something.
    Last edited by Norm; June 24th, 2012 at 05:19 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    If you print the bounds of the components from their paint()
    Just to be sure how do I print out those bounds? Inside the paintComponent method?

    public void paintComponent(Graphics draw2D) {
    ............
    ............
    System.out.println(..........);
    } ???

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    System.out.println("<NAMEOFCLASS> bnds=" + getBounds());

    Since there are 3 classes you should label each so you know which class is being printed.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    What is the MyFrame class used for? It does not have an override for the paint() method but it extends JPanel so I assume it should be showing something.
    myFrame is the frame.... extend JPanels to use

    addKeyListener(this);
    setVisible(true);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    So it does not display anything on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    So it does not display anything on the screen?
    Right nothing,

    I added : System.out.println("myFrame bounds = " + getBounds());
    on the public myFrame constructor and I got :
    myFrame bounds = java.awt.Rectangle[x=0,y=0,width=800,height=600]

    none of all other class printed anything

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Add a paintComponent() method with that println of the bounds to the MyFrame class and see if it is called and prints anything.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Add a paintComponent() method with that println of the bounds to the MyFrame class and see if it is called and prints anything.
    myFrame bounds = java.awt.Rectangle[x=0,y=0,width=792,height=573] It prints it twice.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Did you look at the layout manager part?
    If you give several components to the layout manager and tell it to put them all in the same place, only the last one added will be shown. The ones added earlier will be overlain/replaced by the later ones.

    Your code uses the default so all the components go to the same place and the last one added is the one that is left.

    Change the order that the components are added to see if the printout changes.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Did you look at the layout manager part?
    If you give several components to the layout manager and tell it to put them all in the same place, only the last one added will be shown. The ones added earlier will be overlain/replaced by the later ones.

    Your code uses the default so all the components go to the same place and the last one added is the one that is left.

    Change the order that the components are added to see if the printout changes.
    JFrame frame = new JFrame();
    myFrame s = new myFrame();
    ball b = new ball();
    pad p = new pad();
    frame.add(p);
    frame.add(b);
    frame.add(s);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800,600);

    So I guess that only the frame.add(s); works in there right?...

    So I overrides the first 2?

  20. #20
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    I changed it for :

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    myFrame s = new myFrame();
    ball b = new ball();
    pad p = new pad();
    frame.add(s);
    frame.add(p);
    frame.add(b);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800,600);

    and the ball appeared... but the pad didn't since the ball override the pad....

  21. #21
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    public class arkanoidGame {

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    myFrame s = new myFrame();
    ball b = new ball();
    pad p = new pad();
    panel.add(s);
    panel.add(p);
    panel.add(b);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800,600);
    frame.setVisible(true);
    }
    }

    I Added a panel and the bounds changed to :
    myFrame bounds = java.awt.Rectangle[x=376,y=5,width=10,height=10] Why?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Why?
    One of the magic events caused by a layout manager. The JPanel has a different default layout manager from the JFrame.

    You need to figure out what layout you want to use for those 3 components.
    Have you seen the tutorial:
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    One of the magic events caused by a layout manager. The JPanel has a different default layout manager from the JFrame.

    You need to figure out what layout you want to use for those 3 components.
    Have you seen the tutorial:
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Well the ball as to go anywhere....

    The pad can stay at the bottom

    And the bricks at the top...

    3 Different Layout?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Yes, you can use different layout managers in different containers. It all depends on what you want the GUI to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Ok!, I got rid of my myFrame class I think it was pretty much useless so now I got my arkanoidGame ,pad and ball Class

    I tryed the System.out.println in the addComponentsToPane and it gives me
    myFrame bounds = java.awt.Rectangle[x=0,y=0,width=0,height=0]

    So I don't really know why It's 0.......I did set the size and bounds in createAndShowGui

    import java.awt.*;
    import javax.swing.*;
     
     
    public class arkanoidGame extends JFrame {
    	FlowLayout layout = new FlowLayout();
     
    	private static final long serialVersionUID = 1L;
     
     
    	public arkanoidGame(String name) {	
    		super(name);
     
    	}
    	public void addComponentsToPane(final Container pane) {
     
    		final JPanel panel = new JPanel();		
    		panel.setLayout(layout);
    		panel.add(new ball());
    		panel.add(new pad());	
    		System.out.println("myFrame bounds = " + getBounds());
    	}
     
    	private static void createAndShowGui() {
    		arkanoidGame frame = new arkanoidGame("Arkanoid Game");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.addComponentsToPane(frame.getContentPane());				
    		frame.pack();
    		frame.setSize(800,600);
    		frame.setBounds(0,0,800,600);	
    		frame.setFocusable(true);
    		frame.setFocusTraversalKeysEnabled(false);
    		frame.setVisible(true);		
    	}
     
    	public static void main(String[] args) {
    		try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
    		  UIManager.put("swing.boldMetal", Boolean.FALSE);
     
    	        javax.swing.SwingUtilities.invokeLater(new Runnable(){
    	            public void run() {
    	                createAndShowGui();
    	            }	
     
    	        });
    	}
    }

Page 1 of 3 123 LastLast

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. Table with CustomModel when clicked shows old entry
    By Nesh108 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 9th, 2011, 06:38 AM
  3. Mutiple JFrames Problems
    By mDennis10 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 3rd, 2011, 10:24 PM
  4. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM