Results 1 to 2 of 2
  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Jpanel in layered panel.

    Hi ,
    I saw an interesting code block to scycle through panels.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class Main extends JPanel {
    
        private static final Random random = new Random();
        private static final JPanel cards = new JPanel(new CardLayout());
        private final String name;
    
        public Main(String name) {
            this.name = name;
            this.setPreferredSize(new Dimension(320, 240));
            this.setBackground(new Color(random.nextInt()));
            this.add(new JLabel(name));
        }
    
        @Override
        public String toString() {
            return name;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            for (int i = 1; i < 3; i++) {
                Main p = new Main("Panel " + String.valueOf(i));
                cards.add(p, p.toString());
            }
            JPanel control = new JPanel();
            control.add(new JButton(new AbstractAction("\u22b2Prev") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.previous(cards);
                }
            }));
            control.add(new JButton(new AbstractAction("Next\u22b3") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.next(cards);
                }
            }));
            f.add(cards, BorderLayout.CENTER);
            f.add(control, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    I was wondering i f it would be possible to do something like this for JPanels, so I could cycle through menus for a game.
    Thanks

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

    Default Re: Jpanel in layered panel.

    Quote Originally Posted by dd2308 View Post
    I was wondering i f it would be possible to do something like this for JPanels, so I could cycle through menus for a game.
    Thanks
    Yes, it is quite possible and quite commonly done. Google for the CardLayout tutorial (likely the first hit you'll find) and go through it as it will explain how to use this layout with sample code.

Similar Threads

  1. Replies: 1
    Last Post: 03-22-2010, 10:03 AM
  2. Layered Panes
    By teckno101 in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2009, 12:16 AM
  3. transparent gifs and layered panels
    By infinity in forum AWT / Swing
    Replies: 0
    Last Post: 05-01-2009, 09:52 PM
  4. GUI Help(Layered panes, opacity, events)
    By xcallmejudasx in forum New To Java
    Replies: 1
    Last Post: 04-30-2009, 12:11 AM
  5. Layered Pane Demo
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:44 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
  •