欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

CardLayout 演示,cardlayout演示,如何使用CardLayo

来源: javaer 分享于  点击 922 次 点评:23

CardLayout 演示,cardlayout演示,如何使用CardLayo


如何使用CardLayout的一个非常简单的例子。

[Java]代码

import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.Random;/* * Asked so many times here is an example of the use of CardLayout to stack JComponent * one over the other * I tried to make it as simple as possible * We will just stack 20 JLabel with the card number as text for these labels */public class CardLayoutDemo extends JFrame implements ActionListener  {    // the number of Cards to stack one over the other    private static final int NB_CARDS = 20;    // the number of the panel that I display    private int displayedCard;    // the CardLayout will be required by the actionPerformed() method    private CardLayout cardLayout;    // two buttons to change the displayed card    private JButton previous, next;    // the JPanel displaying cards    private JPanel cardPanel;    // just for a touch of fantasy we will generate a radom Color each of the label    private Random ran;    CardLayoutDemo() {        super("CardLayout demo");        // Build the CardLayout object        cardLayout = new CardLayout();        // create a JPanel with that layout        cardPanel = new JPanel(cardLayout);        // add the Panels        ran = new Random();        for(int i = 0; i < NB_CARDS; ++i) {            // create the JLabel            JLabel label = new JLabel("This is card # " + i);            label.setHorizontalAlignment(SwingConstants.CENTER);            // put a random color for the JLabel foreground color            label.setForeground(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));            // add with a String wich is the number of the Card to recall it later            cardPanel.add(label, "" + i);        }        // add the master panel to the JFrame        add(cardPanel, BorderLayout.CENTER);        // two buttons so we can change the card        previous = new JButton("Previous");        next = new JButton("Next");        previous.addActionListener(this);        next.addActionListener(this);        // put them in a JPanel with GridLayout        JPanel p = new JPanel(new GridLayout(1,2));        p.add(previous);        p.add(next);        // add at the bottom of the frame        add(p, BorderLayout.SOUTH);        // make the frame visible        setSize(200, 100);        setVisible(true);        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    }    // called when one of the 2 JButton is clicked    public void actionPerformed(ActionEvent e) {        // if previous button        if(e.getSource() == previous) {            --displayedCard;                // use previous card            if(displayedCard < 0)           // and wrap around                displayedCard = NB_CARDS - 1;        }        else {                              // so it is the next button            ++displayedCard;                // use next card            if(displayedCard == NB_CARDS)   // and wrap around                displayedCard = 0;        }        // ok we know that we have to display displayedCard card        // we make a String out of the displayedCard        cardLayout.show(cardPanel, "" + displayedCard);    }    // to start the whole thing    public static void main(String[] args) {        new CardLayoutDemo();    }}
相关栏目:

用户点评