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.

Results 1 to 9 of 9

Thread: need help with my code!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with my code!

    hey everyone,

    i have been struggling with 2 frames.
    the question here is, how can you let a frame disappear and let another one appear.
    im making a game and when i click on start it needs to show another frame.

    can someone help me with this?

    got the whole code here:

    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class RushhourStart
    {
     
            public static void main(String [] args)
            {
     
                    JFrame frame = new JFrame();
                    frame.setSize(700, 700);
                    frame.setTitle("Rushhour");
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
                    frame.setVisible(true);
     
                    makegame(frame);       
     
            }
     
    public static void makegame(JFrame frame)
     
            {
     
                            JFrame gameframe = new JFrame();
                gameframe.setSize(700, 700);
                gameframe.setLocationRelativeTo(null);
                gameframe.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
     
     
                makeBord panel = new makeBord();
                panel.setLayout(null);
                panel.setBackground(Color.black);
                frame.add(panel);
     
                JButton Start = new JButton("START SPEL");
                Start.setBounds(250, 100, 200, 50);
                panel.add(Start);
                Start.addMouseListener(panel);
     
                JButton Highscore = new JButton("HIGHSCORES");
                Highscore.setBounds(250, 180, 200, 50);
                panel.add(Highscore);
     
                JButton Exit = new JButton("Exit");
                Exit.setBounds(250, 260, 200, 50);
                panel.add(Exit);
     
        }
     
     
    }
     
     
     
     
     
     
    // second class:
     
     
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JPanel;
     
    public class makeBord extends JPanel implements MouseListener {
     
     
            @Override
            public void mouseClicked(MouseEvent arg0) {
     
            }
     
            @Override
            public void mouseEntered(MouseEvent arg0) {
                    // TODO Auto-generated method stub
     
            }
     
            @Override
            public void mouseExited(MouseEvent arg0) {
                    // TODO Auto-generated method stub
     
            }
     
            @Override
            public void mousePressed(MouseEvent arg0) {
                    // TODO Auto-generated method stub
     
            }
     
            @Override
            public void mouseReleased(MouseEvent arg0) {
                    // TODO Auto-generated method stub
     
            }
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with my code!

    Are you sure you aren't looking for CardLayout?

    If you want to hide one JFrame and show another, you do just that: call setVisible(false) on the first JFrame and setVisible(true) on the second JFrame. Which part of that is giving you trouble?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with my code!

    i want the first frame to dissapear when i click on start. it has to open gameframe.

    whats the code for that/solution?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with my code!

    Okay, and can't you call setVisible(false) on your first JFrame and setVisible(true) on your gameframe?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with my code!

    well if i set false on the first jframe, theres no opening menu

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with my code!

    Again, it sounds like you're actually looking for a CardLayout. Have you looked into that at all?

    So your actual question is not how to go from one JFrame to another, but how to add a menu to two JFrames?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help with my code!

    JFrame does not give the option to set the modality, so a JDialog is a much better choice for a second or child window to open from a JFrame. Also, you should be controlling the GUI elements from another class, as in a Model-View-Controller design, so that the program flow is not dependent upon the existence of or controlled by a particular GUI instance that may come and go.

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with my code!

    im going to look into a CardLayout, thanks for helping!

    well greg its for a game, when i press Start it should open a frame and let the other dissapear (or be on top of the other one)

    edit: will look into JDialog as well

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with my code!

    Right, most games don't open in one window, close that window, and then open another window. They simply change what's on the first window. That's where CardLayout comes into play.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM