我在CardLayout上遇到问题。我有一个面板和一个“下一步”按钮。单击它后,我要显示第二个面板。在我的代码中,当我单击“下一步”按钮时,不显示下一个面板。有人可以帮我解决这个问题吗?

package com.test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CardLay extends JFrame {

    private JPanel contentPane;
    private CardLayout ca;

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

    /**
     * Create the frame.
     */
    public CardLay() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        ca =new CardLayout(0, 0);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(ca);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        contentPane.add("1",panel);

        JButton btnNext = new JButton("NEXT");
        btnNext.setBounds(131, 93, 117, 29);
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ca.show(contentPane,"1");
                System.out.println("button clicked");
            }
        });
        panel.add(btnNext);


        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, "name_1353086933711396000");

        JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
        panel_1.add(chckbxNewCheckBox);
    }

}

最佳答案

您需要致电:

ca.show(contentPane, "name_1353086933711396000");


为此,您必须添加第二个面板,如下所示:

contentPane.add("name_1353086933711396000", panel_1);


使用CardLayout时,请确保将导航按钮放在“卡片”本身以外的单独容器中,以便在整个导航过程中都可以看到它们。在这里,您可以在框架的BorderLayout.SOUTH位置放置一个新的导航容器。对于顺序导航,方法previousnext可用。

还要避免使用绝对定位(null布局)。请参见Doing Without a Layout Manager (Absolute Positioning)

public CardLay() {
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   setSize(500, 400);

   ca = new CardLayout(0, 0);

   contentPane = new JPanel();
   contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
   contentPane.setLayout(ca);

   JPanel panel1 = new JPanel();
   panel1.add(new JButton("Test Button"));
   contentPane.add("card1", panel1);

   JPanel panel2 = new JPanel();
   contentPane.add("card2", panel2);
   JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
   panel2.add(chckbxNewCheckBox);

   JPanel navigationPanel = new JPanel();

   JButton btnPrevious = new JButton("< PREVIOUS");
   btnPrevious.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ca.previous(contentPane);
    }
   });
   navigationPanel.add(btnPrevious);

   JButton btnNext = new JButton("NEXT >");
   btnNext.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
      ca.next(contentPane);
    }
   });
   navigationPanel.add(btnNext);

   add(contentPane);
   add(navigationPanel, BorderLayout.SOUTH);
}


推荐:How to Use CardLayout

08-27 15:37