简体   繁体   中英

Trying to add a JPanel that uses CardLayout to a JFrame

I can't spot the mistake, when I run I get a blank frame

I am trying to make an applet where the first screen you see contains 4 buttons, one for each sorting algorithm I have to implement, then once the button has been clicked it takes you to a new panel that has a graphical representation of an array being sorted (I have already done this in a smaller applet)

Here is my code:

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




    }

}

And the controller 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 hasnt been added to panel containing the components namely newCLayoutTest

add(holderPanel);

Note there's no need to subclass JPanel as you're not adding any new functionality to the panel. You can simply create a panel and add the components. Also follow Java Naming conventions ie use upper-case initial letters for class names, eg NewCLayoutTest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM