Go back to prior JPanel

后端 未结 1 1626
既然无缘
既然无缘 2021-01-27 04:50

I have inherited code and for reasons to long to explain I am required to use a null layout. I have been attempting to take what they have an navigate between JPanels. I haven\'

相关标签:
1条回答
  • 2021-01-27 05:13

    I am required to use a null layout.

    If you are asked by some mentor to perform navigation among panels using null layout, here is what you need to do:

    • First, leave the mentor.
    • Second, use CardLayout.

    Check out the tutorial: How to use CardLayout

    Working Example:

    here is a written CardLayoutDemo which navigates among 10 panels including Home panel using button click action:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class CardLayoutDemo1 extends JFrame {
    
        private JPanel jPanel1;
        private JButton navHomeButt;
        private JButton navNextButt;
        private JButton navPreviousButt;
        private JPanel panelContainer;
    
        public CardLayoutDemo1() {
            initComponents();
    
    
            panelContainer.add(createSamplePanel("Home Panel "), ""+0);
            for(int i=1; i < 10; i++)
            {
               panelContainer.add(createSamplePanel("Panel "+i), ""+i);
            }
        }
    
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            navPreviousButt = new JButton();
            navNextButt = new JButton();
            navHomeButt = new JButton();
            panelContainer = new JPanel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    
            navPreviousButt.setText("Previous");
            navPreviousButt.setPreferredSize(new Dimension(90, 23));
            jPanel1.add(navPreviousButt);
    
            navNextButt.setText("next");
            navNextButt.setPreferredSize(new Dimension(90, 23));
            jPanel1.add(navNextButt);
    
            navHomeButt.setText("Back to Home");
            jPanel1.add(navHomeButt);
    
    
    
            panelContainer.setPreferredSize(new Dimension(400, 300));
            panelContainer.setLayout(new CardLayout()); 
            // setting the card layout
    
            getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
            getContentPane().add(panelContainer, BorderLayout.CENTER);
    
    
    
    
            navNextButt.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
                    cardLayout.next(panelContainer);
                    // using cardLayout next() to go  to next panel
                }
            });
            navHomeButt.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
                   cardLayout.first(panelContainer);
                   // suing first to get to the home panel
                }
            });
            navPreviousButt.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
                   cardLayout.previous(panelContainer);
    
                   // using previous to get to previous(left)panel
                }
            });
    
            pack();
        }                   
    
        public JPanel createSamplePanel(String panelTitle)
        {
            JPanel samplePanel = new JPanel();
            samplePanel.add(new JLabel(panelTitle));
    
            return samplePanel;
    
        }                                           
    
    
        public static void main(String args[]) {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CardLayoutDemo1().setVisible(true);
                }
            });
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题