Results 1 to 5 of 5

Thread: JFrame help

  1. #1
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default JFrame help

    whenever i run this, it either opens with a white screen or it opens with the stuff behind it on the screen, and whenever i minimize it and unminimize it, the screen is black.
    any suggestions?

    Java Code:
    package firstgame;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    
    public class Game extends JFrame{
    	
    	private static final Color BLUE = null;
    
    	public Game(){
    		
    		setTitle("First Game");
    		setBackground(BLUE);
    		setSize(640,480);
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	public static void main(String[] args){
    		new Game();
    	}
    	
    	public void paint(Graphics g){
    	g.drawString("hello", 5, 40);
    	}
    	
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: JFrame help

    Read the API documentation for the JFrame class: unlike a Frame, you can't draw on a JFrame; you draw on its ContentPane instead. You can supply your own ContentPane (usually a JPanel) and draw on it.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: JFrame help

    Ok thanks! I found the api documentation, and am reading it now!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: JFrame help

    Quote Originally Posted by Etimer View Post
    Ok thanks! I found the api documentation, and am reading it now!
    Good; it's almost lik a mantra: first read the API documentation and understand it and only then start designing and coding.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: JFrame help

    There's also a very god tutorial for Swing: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    The part you'll need to go through right away is Performing Custom Painting.

    Moving this thread to AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 11-27-2011, 06:18 AM
  2. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •