Results 1 to 5 of 5
  1. #1
    gregsakas is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default If I create multiple frames within a method, will it work?

    I'm using this code:
    Java Code:
    public void CreateFrame() {
    	Graphics g=getGraphics();
    	JFrame frame=new JFrame("Test");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add("", frame);
    	g.drawImage(mole11, 128, 104, frame);
    	frame.pack();
    	frame.setVisible(true);
    }
    to create multiple frames. Say I create nine frames using the method without closing any of them, will it work properly?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: If I create multiple frames within a method, will it work?

    What makes you think that it will not work? Try it for your self :)

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

    Default Re: If I create multiple frames within a method, will it work?

    Why are you adding a JFrame to another JFrame's contentPane? Why are you drawing with an unstable Graphics object obtained via getGraphics()? What's the driving force behind all of this?

  4. #4
    gregsakas is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: If I create multiple frames within a method, will it work?

    Quote Originally Posted by Fubarable View Post
    Why are you adding a JFrame to another JFrame's contentPane? Why are you drawing with an unstable Graphics object obtained via getGraphics()? What's the driving force behind all of this?
    I am new at programming and was tasked with creating a whack-a-mole program using frames, something i have never learned before.

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

    Default Re: If I create multiple frames within a method, will it work?

    Quote Originally Posted by gregsakas View Post
    I am new at programming and was tasked with creating a whack-a-mole program using frames, something i have never learned before.
    What you have posted in your original post is not how you'd do it.

    All Swing solutions would involve you creating one and only one single JFrame. A possible solution could be where the JFrame has a JPanel that holds a grid of JLabels in a GridLayout, and then change the ImageIcons held by the JLabels for when you want a mole to appear and disappear. A Swing Timer and the Random class could help you with the animation and the random selection of mole holes.
    Last edited by Fubarable; 08-01-2012 at 04:46 PM.

Similar Threads

  1. Resizing of frames doesn't work.
    By Amanoo in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2012, 07:46 AM
  2. 4 frames work toghether
    By Shima in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-09-2010, 04:14 AM
  3. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 04:18 AM
  4. Replies: 2
    Last Post: 12-24-2009, 07:41 PM
  5. will java webBrowser work with frames
    By elipford in forum New To Java
    Replies: 2
    Last Post: 06-04-2007, 04:26 PM

Tags for this Thread

Posting Permissions

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