Orobo Lucky Ozoka
Orobo Lucky Ozoka

Reputation: 43

Layout Manager usage

I was trying to create a simple card display program using layout managers but i still haven't got the hang of using Layout Mangers properly. Here is my code, it uses BorderLayout(); as the layout manager for the contentPane, GridLayout(); for the buttonPanel, CardLayout(); for the cardPanel, and FlowLayout(); for the labelPanel.

public class TestClass // implements ActionListener 
{
    static JPanel content = new JPanel(new BorderLayout());
    static JPanel cardPanel = new JPanel();
    static CardLayout cardLayout;
    static JPanel buttonPanel = new JPanel(new FlowLayout());
    static JPanel labelPanel = new JPanel(new GridLayout(9,1));
    static JButton jack, king, queen, ace;
    static JButton first, last, prev, next;

   public static void main(String[] args) {
        JFrame window = new JFrame("Window");

        //------Creates the Label Panel --//
        JLabel label = new JLabel("SELECT A CARD");
        label.setFont(new Font("Georgia",Font.BOLD,18));
        labelPanel.add(label, FlowLayout.LEFT);

        //-----Creates the Button Panel --//
        jack = new JButton("Jack");
        queen = new JButton("Queen");
        king = new JButton("King");
        ace = new JButton("Ace");
        first = new JButton("First");
        last = new JButton("Last");
        prev = new JButton("Previous");
        next = new JButton("Next");
        JLabel sep = new JLabel("-------");
        buttonPanel.add(jack);
        buttonPanel.add(queen);
        buttonPanel.add(king);
        buttonPanel.add(ace);
        buttonPanel.add(sep);
        buttonPanel.add(first);
        buttonPanel.add(last);
        buttonPanel.add(prev);
        buttonPanel.add(next);

        //-------Creates the cardPanel --//
        JLabel card1 = new JLabel("JACK");
        JLabel card2 = new JLabel("QUEEN");
        JLabel card3 = new JLabel("KING");
        JLabel card4 = new JLabel("ACE");
        cardPanel.add(card1, "jack");
        cardPanel.add(card2, "queen");
        cardPanel.add(card3, "king");
        cardPanel.add(card4, "ace");
        cardPanel.setLayout(cardLayout = new CardLayout());
        cardLayout.first(cardPanel);

        // ----- Adds the panels to the contentPane ---//
        content.add(labelPanel, BorderLayout.NORTH);
        content.add(buttonPanel, BorderLayout.WEST);
        content.add(cardPanel, BorderLayout.CENTER);

        window.setContentPane(content);
        window.setLocation(300,100);
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        window.setResizable(true);
    }
}

but the output isn't as expected. I would want the label at the top left, the buttons at the left (and layed out using Grid), and then the cardLabel at the center.

EDITED: Problem solved, I made a silly mistake

    static JPanel buttonPanel = new JPanel(new GridLayout(9,1)); //previously FlowLayout
    static JPanel labelPanel = new JPanel(new FlowLayout()); //previously GridLayout

Upvotes: 0

Views: 49

Answers (1)

Jase Pellerin
Jase Pellerin

Reputation: 397

The only thing in your grid layout (labelPanel) is this:

JLabel label = new JLabel("SELECT A CARD");

Everything else is in buttonPanel, which is a flow layout.

Check this out: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Upvotes: 1

Related Questions