尝试将使用 CardLayout 的 JPanel 添加到 JFrame

Trying to add a JPanel that uses CardLayout to a JFrame

我无法发现错误,当我 运行 我得到一个空白帧时

我正在尝试制作一个小程序,其中您看到的第一个屏幕包含 4 个按钮,每个按钮对应我必须实现的每个排序算法,然后单击该按钮后,它会将您带到一个具有图形的新面板正在排序的数组的表示(我已经在一个较小的小程序中完成了)

这是我的代码:

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

import javax.swing.*;

public class newCLayoutTest extends JPanel{

    private JPanel holderPanel, mainPanel, bubblePanel, selectionPanel, mergePanel, quickPanel;
    private JButton bubbleButton, selectionButton, mergeButton, quickButton;

    private CardLayout cardLayout = new CardLayout();

    public newCLayoutTest()
    {
        JLabel label = new JLabel("Label");
        holderPanel = new JPanel();
        mainPanel = new JPanel();
        bubblePanel = new JPanel();
        selectionPanel = new JPanel();
        mergePanel = new JPanel();
        quickPanel = new JPanel();

        holderPanel.setLayout(cardLayout);

        bubbleButton = new JButton();
        selectionButton = new JButton();
        mergeButton = new JButton();
        quickButton = new JButton();

        //mainPanel.setLayout(new GridLayout(2,2));
        mainPanel.add(bubbleButton);
        mainPanel.add(selectionButton);
        mainPanel.add(mergeButton);
        mainPanel.add(quickButton);

        bubblePanel.add(label);

        holderPanel.add(mainPanel, "1");
        holderPanel.add(bubblePanel, "2");

        cardLayout.show(holderPanel,"2");




    }

}

和控制器class:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class testControl extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {


        public void run() {

            newCLayoutTest panel = new newCLayoutTest();
            JFrame frame = new JFrame("LET THIS WORK");

        //  frame.setLayout(new BorderLayout());

            frame.add(panel);
            //frame.getContentPane().add(panel);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setSize(600, 400);
            frame.setVisible(true);
        }
    });
}
}

holderPanel 尚未添加到包含 newCLayoutTest

组件的面板
add(holderPanel);

请注意,不需要 subclass JPanel,因为您没有向面板添加任何新功能。您可以简单地创建一个面板并添加组件。还要遵循 Java 命名约定,即对 class 名称使用大写首字母,例如NewCLayoutTest