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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 72

Thread: Mutiple classfile, JPanel nothing shows up

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    I got rid of my myFrame class
    it gives me
    myFrame bounds = java.awt.Rectangle[x=0,y=0,width=0,height=0]
    Not sure where the printout is coming from if you have gotten rid of the myFrame class.

    I'm assuming that the label for the printed message has the name of the class it is in. The idea when debugging with printlns is to have ALL of the printlns use a unique label so you can tell which println it came from.
    Last edited by Norm; June 25th, 2012 at 10:18 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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
    Not sure where the printout is coming from if you have gotten rid of the class?

    in the addComponentsToPane method

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Is the program working now?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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
    Is the program working now?
    No but I got a button showing up

    controls.add(startButton);
     
    		startButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				panel.validate();
    				panel.repaint();
    				System.out.println("Layout bounds = " + getBounds());
    			}
    		});
    		pane.add(controls,BorderLayout.SOUTH);

    With that I got a button and every time I press it tells me the bounds
    Layout bounds = java.awt.Rectangle[x=0,y=0,width=800,height=600]

    But still no ball or pad

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    still no ball or pad
    Do you mean that the paintComponent() method of those two classes is not being called?

    Does the button's bounds take up the whole screen?
    How are you using a layout manager to control where the components are positioned on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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
    Do you mean that the paintComponent() method of those two classes is not being called?
    Right none of those two classes are being called

    Quote Originally Posted by Norm View Post
    Does the button's bounds take up the whole screen?
    No the button is a nice size and it's at the bottom of the screen


    Quote Originally Posted by Norm View Post
    How are you using a layout manager to control where the components are positioned on the screen?
    pane.add(controls,BorderLayout.SOUTH);

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Please post the current code.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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

    arkanoidGame.java

    import java.awt.*;
    import java.awt.event.*;
     
     
    import javax.swing.*;
     
     
    public class arkanoidGame extends JFrame {
    	FlowLayout layout = new FlowLayout();
    	JButton startButton = new JButton("Start Game");
     
     
    	private static final long serialVersionUID = 1L;
     
     
    	public arkanoidGame(String name) {	
    		super(name);
     
    	}
    	public void addComponentsToPane(final Container pane) {
     
    		final JPanel panel = new JPanel();	
    		JPanel controls = new JPanel();
    		controls.setLayout(new FlowLayout());
    		panel.setLayout(layout);		
    		panel.add(new ball());
    		panel.add(new pad());		
    		controls.add(startButton);
     
    		startButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				panel.validate();
    				panel.repaint();
    				System.out.println("pad bounds = " + getBounds());
    			}
    		});
    		pane.add(controls,BorderLayout.SOUTH);
    		pane.add(new ball(),BorderLayout.NORTH);
    		pane.add(new pad(),BorderLayout.CENTER);
    	}
     
     
    	private static void createAndShowGui() {
    		arkanoidGame frame = new arkanoidGame("Arkanoid Game");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);					
    		frame.pack();
    		frame.setSize(800,600);
    		frame.setBounds(0,0,800,600);	
    		frame.addComponentsToPane(frame.getContentPane());	
    		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();
     
    	            }	
     
    	        });
    	}
    }

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    When I execute the code I get this:
    pad paint bnds=java.awt.Rectangle[x=0,y=10,width=784,height=516]
    Ball paint bnds=java.awt.Rectangle[x=0,y=0,width=784,height=10]

    showing that the paintComponent methods are called and I get two lines (blue and red) drawn on the screen.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    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
    When I execute the code I get this:
    pad paint bnds=java.awt.Rectangle[x=0,y=10,width=784,height=516]
    Ball paint bnds=java.awt.Rectangle[x=0,y=0,width=784,height=10]
    I get the same thing

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    I'm confused. In post #29 you said: But still no ball or pad

    If those messages are being printed then the ball and pad class's methods are being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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
    I'm confused. In post #29 you said: But still no ball or pad

    If those messages are being printed then the ball and pad class's methods are being executed.
    I added this since :

    pane.add(new ball(),BorderLayout.NORTH);
    pane.add(new pad(),BorderLayout.CENTER);

    I think that's why we get some random bounds...

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    The objects for the ball and pad classes are being added and their paintComponent methods are being called.

    What is the problem now?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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
    The objects for the ball and pad classes are being added and their paintComponent methods are being called.

    What is the problem now?
    They are not showing up

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Do you have the call to drawLine() I mentioned in post#4 in the paintComponent() method to show where the component is located?

    What do you expect to show up? How does the drawing x,y values compare to the bounds of the component? Are they outside or inside?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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
    Do you have the call to drawLine() I mentioned in post#4 in the paintComponent() method to show where the component is located?

    What do you expect to show up? How does the drawing x,y values compare to the bounds of the component? Are they outside or inside?
    I added the drawLine() and the lines show up.

    The problem is the bound of each they are not right... I tryed with the method setBounds but it doesn't seem to work

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    the bound of each they are not right
    How are you trying to control the bounds of the components?
    The code uses two layout managers. They will position the components. You cant use the setBounds() method in the same container that there is a layout manager.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    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
    How are you trying to control the bounds of the components?
    The code uses two layout managers. They will position the components. You cant use the setBounds() method in the same container that there is a layout manager.
    How can I do it then? Can I put them where ever I want them to be?

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Can I put them where ever I want them
    Set the layout manager to null and use setBounds()
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    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
    Set the layout manager to null and use setBounds()
    Can i have overlapping bounds?

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Yes. The results would be like laying one piece of paper on another one. The top one will be visible, the one underneath will be hidden.

    Perhaps if you made the top ones transparent you could see what is underneath.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    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
    Set the layout manager to null and use setBounds()

    I'm trying to use setBounds it doesn't work I tryed to use it that way :


    pane.add(new ball().setBounds(60,60,60,60));

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    What does "doesn't work" mean? Please explain in more detail.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    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 does "doesn't work" mean? Please explain in more detail.

    It's an error :
    The method add(Component) in the type Container is not applicable for the arguments (void)

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,085
    Thanks
    48
    Thanked 2,150 Times in 2,123 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Break the statement up into single steps.

    setBounds() is a void method. You can not add(void) to a container.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 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, 02: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, 05: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, 08: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