1

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);
        }
    });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49

1 Answers1

2

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 i.e. use upper-case initial letters for class names, e.g. NewCLayoutTest

Reimeus
  • 158,255
  • 15
  • 216
  • 276