Results 1 to 3 of 3
  1. #1
    Zorobay is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default JPanel not visible? Am I stupid!?

    Hi, I got this simple code:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class paint extends JFrame{
    	
    	JPanel panel = new JPanel();
    	public void paint(Graphics g){
    		g.drawRoundRect(20, 20, 50, 50, 10, 10);
    	}
    	
    	public static void main (String[] args){
    		paint p = new paint();
    	}
    	
    	public paint(){
    		getContentPane().setLayout(null);
    		setSize(300, 200);
    		setBackground(Color.WHITE);
    		setForeground(Color.BLACK);
    		panel.setBackground(Color.RED);
    		panel.setSize(300,200);
    		panel.setVisible(true);
    		add(panel);
    		setVisible(true);
    	}
    }
    Now why the hell can't i see the JPanel?! I have dont almost the exact same thing in another program, and it works perfectly =( I can see the drawn "roundrect", but there is no background. Also, why does setBackground, and setForeground never work on JFrame??

    TY!

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: JPanel not visible? Am I stupid!?

    try these bounds:

    g.drawRoundRect(20, 50, 50, 50, 10, 10);

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JPanel not visible? Am I stupid!?

    Please read the Swing tutorials first. You are doing so many things wrong I have to wonder if you haven't done this most basic step yet.

    For example, you are
    • Drawing right on the JFrame itself instead of in a JPanel's paintComponent method
    • Covering up you're what you're drawing by putting a component on top of it
    • Using wrong Java naming conventions
    • trying to set component's sizes directly rather than preferred size and calling pack() on the JFrame
    • setting background and foreground colors of a JFrame (this does nothing at all useful)
    • ...


    Also please search this forum for sample code that shows drawing in Swing applications. There are many such examples around here, I know, because I've written a few.
    Last edited by Fubarable; 03-23-2012 at 11:28 PM.

Similar Threads

  1. Stupid mistake, maybe?
    By knox in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 09:39 AM
  2. Replies: 0
    Last Post: 02-16-2011, 12:16 AM
  3. I need an idea with something really stupid
    By blf_titi in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-27-2010, 04:24 AM
  4. Stupid error
    By dewitrydan in forum Java Applets
    Replies: 3
    Last Post: 08-09-2010, 02:29 PM
  5. Problems with setting the menubar and a JPanel visible
    By leitbug06 in forum AWT / Swing
    Replies: 1
    Last Post: 03-07-2009, 12:46 AM

Posting Permissions

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