Go back to prior JPanel

2019-02-27 20:02发布

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't been able to figure out how. This is what I have now which compiles broken down into a SSCCE below. What I am attempting to do is add the JPanels to an ArrayList which contains a reference to previous JPanels. That way I can call a "home" JPanel from the current JPanel the user is in. As of now it goes to the previous JPanel but the contents are empty. Any help would be great, THANKS!

import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Mainscreen extends JFrame {

     public JPanel Home;


     public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
                Mainscreen frame = new Mainscreen();
                frame.setVisible(true);
                 } 
            catch (Exception e) {
                e.printStackTrace();
                }
            }
        });
     }


     public Mainscreen() {
        ArrayList <JPanel> jpLayout = new ArrayList();
    final Dataentrylog DEL = new Dataentrylog(this, jpLayout);  

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setBounds(100, 100, 618, 373);
        Home=new JPanel();
        Home.setBackground(new Color(255, 250, 250));
        Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
        Home.setVisible(true);
        setContentPane(Home);

            Home.setLayout(null);     

              JButton delLog = new JButton("Next JPanel");
              delLog.setFont(new Font("Tahoma", Font.PLAIN, 14));
              delLog.setForeground(new Color(0, 0, 0));
              delLog.setBackground(UIManager.getColor("Menu.selectionBackground"));
              Home.add(delLog);

              jpLayout.add(Home);
              delLog.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {

                Home.setVisible(false);
                setContentPane(DEL);
                getContentPane().setLayout(null);    

              }
              });
              delLog.setBounds(44, 214, 213, 61);      

     }


    } 

import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Dataentrylog extends JPanel {


 public Dataentrylog(final JFrame parent, final ArrayList <JPanel> jpLayout) {
    setBounds(100, 100, 618, 373);
    setBackground(new Color(255, 250, 250));
    setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
    setLayout(null);



    final JButton btnSignIn = new JButton("Go Back");
    btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
    btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
    btnSignIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            setVisible(false);
            parent.setContentPane(jpLayout.get(0));
            setLayout(null);

     }
    });
    btnSignIn.setBounds(226, 282, 153, 52);
    add(btnSignIn);

 }
} 

1条回答
Animai°情兽
2楼-- · 2019-02-27 20:41

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);
            }
        });
    }

}
查看更多
登录 后发表回答