简体   繁体   中英

Trouble using Cardlayout in java

Having Trouble adding components on panels in cardlayout , they are appearing weirdly (very small and top center) , have tried many layouts but not getting appropriate results , i have to place , buttons , Split Panes , tab Panes on different panels here is the sample code. Having same issue on the code which im working right now

pls see where im going wrong

public static void main(String[] args) {

    CardLayout cards = new CardLayout();
    JPanel cardPanel = new JPanel();
    cardPanel.setLayout(cards);


    JFrame guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Frame");
    guiFrame.setSize(528, 555);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);
    guiFrame.setVisible(true);

    JButton B_1 = new JButton("");
    JButton B_2 = new JButton("");


    JPanel firstCard = new JPanel();

    firstCard.add(B_1);

    B_1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            cards.next(cardPanel);
        }
    });


    JPanel secondCard = new JPanel();

    secondCard.add(B_2);

    B_2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            cards.previous(cardPanel);
        }
    });

    cardPanel.add(firstCard);
    cardPanel.add(secondCard);

    guiFrame.add(cardPanel);

}
}

Your code wouldn't even compile.

I rearranged your code and added the following features:

  1. I started your GUI with a call to SwingUtilities invokeLater. This puts the Swing GUI on the Event Dispatch thread (EDT).

  2. I put like code together (JFrame, JPanel) so that the code would be easier to follow.

  3. I put a JLabel on each of the card panels so you could see which panel was which.

  4. I put text in the JButtons. They were so small because you had no text or label for the buttons to display.

  5. I moved some of the JFrame code to the end of the method. The last thing you do in a Swing GUI is set the frame visible. You have to construct all of the Swing components before you make the JFrame window visible.

Here's the working, tested, minimal example of a card layout that you were trying to code.

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutTest implements Runnable {

    @Override
    public void run() {
        JFrame guiFrame = new JFrame();

        // make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Frame");

        final CardLayout cards = new CardLayout();
        final JPanel cardPanel = new JPanel();
        cardPanel.setLayout(cards);

        final JPanel firstCard = new JPanel();
        firstCard.setLayout(new FlowLayout());

        JLabel label1 = new JLabel("Panel 1");
        firstCard.add(label1);

        JButton b_1 = new JButton("Swap to Panel 2");
        firstCard.add(b_1);

        final JPanel secondCard = new JPanel();
        secondCard.setLayout(new FlowLayout());

        JLabel label2 = new JLabel("Panel 2");
        secondCard.add(label2);

        JButton b_2 = new JButton("Swap to Panel 1");
        secondCard.add(b_2);

        b_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                 cards.next(cardPanel);
            }
        });

        b_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                 cards.previous(cardPanel);
            }
        });

        cardPanel.add(firstCard, "First Panel");
        cardPanel.add(secondCard, "Second Panel");

        guiFrame.add(cardPanel);
        guiFrame.setSize(528, 555);
        // This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        guiFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutTest());
    }

}

This is how CarLayout Works, by default, it puts object in horisontal center and vertical top. If you did not specified size of added component, it is resized to smallest size possible. Configure your layout alignment, set sizes on your components, or use another layout.

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