1

我继承了代码,并且出于长时间解释的原因,我需要使用空布局。我一直在尝试了解他们在 JPanel 之间的导航。我一直无法弄清楚如何。这就是我现在所拥有的,它编译成下面的 SSCCE。我正在尝试做的是将 JPanel 添加到包含对以前 JPanel 的引用的 ArrayList 中。这样我就可以从用户所在的当前 JPanel 调用“家”JPanel。到目前为止,它转到了前一个 JPanel,但内容是空的。任何帮助都会很棒,谢谢!

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

 }
} 
4

1 回答 1

5

我需要使用空布局。

如果某些导师要求您使用 执行面板之间的导航null layout,您需要执行以下操作:

  • 首先,离开导师。
  • 其次,使用 CardLayout。

查看教程:如何使用 CardLayout

工作示例:

这是一篇使用按钮单击操作CardLayoutDemo在包括主页面板之间导航的文章:10 panels

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

}
于 2013-10-24T21:58:16.000 回答