Results 1 to 5 of 5
  1. #1
    oblivionkey21 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Change Frame and Focus in Card Layout

    I'm nearing the final development stages of my first game, but have run into a problem. I've constructed 4 different levels and allow the user to select the level they want to play from a central JPanel in a Card Layout system. My problem is that once a level is completed, I can't switch the JPanel which is displayed to start the next level, since I don't know how to access the original JPanel which acts as a driver for the other panels. Any help is appreciated, and my code is posted bellow. Also, please use code in your example since I'm still pretty new to Java. Thanks.

    MainFrame.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Font;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
       public class MainFrame extends JFrame implements ActionListener
          {
             JPanel bodyPanel;
             JPanel panel1,panel2,panel3,panel4,panel5;
             JButton button1,button2,button3,button4,button5;
             JLabel title;
             Container con;
             CardLayout clayout;
    
    
          public MainFrame() 
       {
        clayout=new CardLayout();
        bodyPanel=new JPanel(clayout);
    
        button1=new JButton("Level 1");
        button3= new JButton("Level 2");
        button4 = new JButton("Level 3");
        button2=new JButton("Exit");
        button5 = new JButton("Level 4");
    
    
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        button4.addActionListener(this);
        button5.addActionListener(this);
    
    
        panel1=new JPanel();
            title = new JLabel("Space Adventure");
            panel1.add(title);
            title.setFont(new Font("Serif", Font.PLAIN, 110));
            panel1.add(button1);
            panel1.add(button3);
            panel1.add(button4);
            panel1.add(button5);
            panel1.add(button2);
    
    
    
        panel1.setBackground(Color.pink);
        panel2=new PrizePanel();
        panel2.setBackground(Color.gray);
    
    
    
        bodyPanel.add(panel1,"one");    
        bodyPanel.add(panel2,"two");    
    
    
        setLayout(new BorderLayout());
        setSize(800,400);
        add(bodyPanel,BorderLayout.CENTER);
        bodyPanel.setBounds(0,100, 600, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    
    }
    
          public static void main(String args[])
          {
              new MainFrame();
          }
    
          public void actionPerformed(ActionEvent e) {
    
              if(e.getSource()==button1)
              {
               clayout.show(bodyPanel, "two");
                  panel2.requestFocusInWindow();
               }else if(e.getSource()==button3)
              {
              panel3=new PrizePanel2();
              panel3.setBackground(Color.gray);
              bodyPanel.add(panel3,"three");
              clayout.show(bodyPanel, "three");
              panel3.requestFocusInWindow();
               }
               else if(e.getSource()==button4)
              {
              panel4=new PrizePanel3();
              panel4.setBackground(Color.gray);
              bodyPanel.add(panel4,"four");
              clayout.show(bodyPanel, "four");
              panel4.requestFocusInWindow();
               }else if(e.getSource()==button5)
              {
              panel5=new PrizePanel4();
              panel5.setBackground(Color.gray);
              bodyPanel.add(panel5,"five");
              clayout.show(bodyPanel, "five");
              panel5.requestFocusInWindow();
               }
    
    
              else if(e.getSource()==button2)
              {
                 System.exit(0);
              }
    
          }
    
    }

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Change Frame and Focus in Card Layout

    I am not sure if am going to help you .. cause i took a fast look at your code but when i want to change JPanels in my JFrames i use this way :
    Java Code:
    	public void actionPerformed(ActionEvent e) {
    		contain = this.getContentPane();
    		contain.remove(currentPanel);
    		
    		if(e.getSource() == button1){
    			currentPanel = firstPanel;
    		}
    		if(e.getSource() == button2){
    			currentPanel = secondPanel;
    		}
    		contain.add(currentPanel);
    		contain.validate();
    		contain.repaint();
    	}
    	
    	// which contain is a Container of the class

  3. #3
    oblivionkey21 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Change Frame and Focus in Card Layout

    I appreciate your help, but I don't think that that'll work. I can already switch panels from the MainFrame screen, but once I move to a level screen, I don't have access to the mainframe anymore and can't tell it to switch levels.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,842
    Rep Power
    5

    Default Re: Change Frame and Focus in Card Layout

    Quote Originally Posted by oblivionkey21 View Post
    but once I move to a level screen, I don't have access to the mainframe anymore and can't tell it to switch levels.
    So it will work when you solve this little limitation.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    oblivionkey21 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Change Frame and Focus in Card Layout

    I solved it. I just had to make a static swap function. Since it was static, I could call it from any class. Thanks to everyone who took the time to look over this thread.

    Java Code:
    public static void swap(int x){
          switch(x){
             case 1:
               clayout.show(bodyPanel, "two");
                  panel2.requestFocusInWindow();
                  break;
               case 2:
              clayout.show(bodyPanel, "three");
              panel3.requestFocusInWindow();
               break;
               case 3:
               panel3 = null;
              clayout.show(bodyPanel, "four");
              panel4.requestFocusInWindow();
              break;
               case 4:
               panel4=null;
              clayout.show(bodyPanel, "five");
              panel5.requestFocusInWindow();
               break;
             }
          }
    Last edited by oblivionkey21; 02-19-2014 at 12:10 AM. Reason: I found the answer

Similar Threads

  1. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 05:11 PM
  2. Image in Card Layout
    By poorbrain in forum Java Applets
    Replies: 4
    Last Post: 03-08-2011, 06:15 PM
  3. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  4. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  5. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 09:37 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
  •