简体   繁体   中英

How to use Card layout?

I'm having a hard time trying to figure out what is a card layout. I read so many articles and implemented this small example of to see how card layout works. But I can't understand some methods(which are commented). Can someone please help me (I use commandline).

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class C_layout implements ActionListener
{
    JButton b2;
    JButton b1;
    JFrame f1;
    JPanel card1;
    JPanel card2;
    JPanel Jp;
    void Example()
    {
    f1=new JFrame("CardLayout Exercise");
    f1.setLocationRelativeTo(null);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setSize(500,500);
    f1.setVisible(true);

    Container cont=f1.getContentPane();
    cont.setLayout(null);

    Jp=new JPanel(new CardLayout()); //<-- How to implement card layout here (MAIN PANEL)
    f1.add(Jp);
    Jp.setLayout //<-- Not sure what means here ERROR
    card1=new JPanel(); // First panel
    Jp.add(card1);
    card2=new JPanel(); // Second panel
    Jp.add(card2);

    JLabel lb1=new JLabel("This is the first Panel");
    lb1.setBounds(250,100,100,30);
    card1.add(lb1);

    b1=new JButton("NEXT >>");
    b1.setBounds(350,400,100,30);
    b1.addActionListener(this);
    card1.add(b1);


    JLabel lb2=new JLabel("This is the second Panel");
    lb2.setBounds(250,100,100,30);
    card2.add(lb2);

    b2=new JButton("<< PREV");
    b2.setBounds(250,300,100,30);
    b2.addActionListener(this);
    card2.add(b2);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    CardLayout cardLayout = (CardLayout) Jp.getLayout();
    cardLayout.show(card2,"2");
    }
    if(e.getSource()==b2)
    {
    // I still haven't implemented this action listener
    }
    }
}

class LayoutDemo1
{
    public static void main(String[] args)
    {
    C_layout c=new C_layout();
    c.Example();


    }
}

cont.setLayout(null); is bad, bad idea, lose it quickly...

You're going to need a reference to your CardLayout in order to manage it. Start by defining a instance field of CardLayout ...

private  CardLayout cardLayout;

Now, create your instance of CardLayout and apply it to your panel...

cardLayout = new CardLayout();
Jp=new JPanel(cardLayout);

This...

Jp.setLayout //<-- Not sure what means here ERROR

doesn't do anything, it's not a valid statement as far as Java is concerned, in fact, it's actually a method, which should take a reference to the LayoutManager you want to use, but since you've already done that when you created the instance of Jp , you don't need it...

You're going to need some way to identify the components you want to show, CardLayout does this via String names, for example...

card1=new JPanel(); // First panel
Jp.add(card1, "card1");
card2=new JPanel(); // Second panel
Jp.add(card2, "card2");

Now, in your ActionListener , you want to ask CardLayout to show the required view...

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==b1)
    {
        cardLayout.show(Jp1,"card2");
    } else if(e.getSource()==b2)
    {
        cardLayout.show(Jp1,"card1");
    }
}

Note, in order for CardLayout#show to work, you need to pace it a reference of the container to which the CardLayout is assigned AND the name of the view you want to display.

See How to Use CardLayout for more details

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