How do I use CardLayout for my Java Program for Login and Menu Items

前端 未结 1 1210
鱼传尺愫
鱼传尺愫 2020-12-12 03:33

I am creating a \"Store\" program that basically can allow an employee to log in with a username and password I provide. After logging in, the employee can then see a \"main

1条回答
  •  余生分开走
    2020-12-12 04:07

    1. Stop using null layouts, seriously, this is the source of more problems then just about anything else with Swing
    2. When adding components to a panel using CardLayout, each panel MUST have a name
    3. In your actionPerformed method, this is not how CardLayout works. To start with, you're creating a new instance of CardLayout, which contains NO components, so how did you expect it to be able to suddenly figure out what to switch to? When using CardLayout#show, the component reference is the parent component that that is begin manged by the CardLayout

    Updated with example

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class MyCardLayout {
    
        public static void main(String[] args) {
            new MyCardLayout();
        }
    
        public MyCardLayout() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JPanel mainPane;
            private JPanel navPane;
            private List pages;
            private int currentPage;
    
            public TestPane() {
                pages = new ArrayList<>(25);
                setLayout(new BorderLayout());
    
                mainPane = new JPanel(new CardLayout());
                navPane = new JPanel();
    
                for (int index = 0; index < 10; index++) {
                    addPageTo(mainPane, "Page" + index, new JLabel("Page " + index, JLabel.CENTER));
                }
    
                JButton btnPrev = new JButton("<<");
                JButton btnNext = new JButton(">>");
    
                navPane.add(btnPrev);
                navPane.add(btnNext);
    
                btnPrev.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setCurrentPage(getCurrentPage() - 1);
                    }
                });
                btnNext.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setCurrentPage(getCurrentPage() + 1);
                    }
                });
    
                add(mainPane);
                add(navPane, BorderLayout.SOUTH);
    
                setCurrentPage(0);
            }
    
            public int getCurrentPage() {
                return currentPage;
            }
    
            protected void setCurrentPage(int page) {
                if (pages.size() > 0) {
                    if (page < 0) {
                        page = pages.size() - 1;
                    } else if (page >= pages.size()) {
                        page = 0;
                    }
    
                    currentPage = page;
                    CardLayout layout = (CardLayout)mainPane.getLayout();
                    layout.show(mainPane, pages.get(currentPage));
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void addPageTo(JPanel pane, String name, Component comp) {
                pages.add(name);
                pane.add(comp, name);
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题