2

我遇到了 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);
    }

}
4

1 回答 1

4

您需要致电:

ca.show(contentPane, "name_1353086933711396000");

为此,您必须像这样添加第二个面板:

contentPane.add("name_1353086933711396000", panel_1);

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

还要避免使用绝对定位(null布局)。请参阅不使用布局管理器(绝对定位)

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

推荐: 如何使用 CardLayout

于 2012-11-16T17:57:24.683 回答