?? cardlayout.java
字號(hào):
/* * @(#)CardLayout.java 1.36 05/03/12 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package java.awt;import java.util.Hashtable;import java.util.Vector;import java.util.Enumeration;import java.io.Serializable;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.ObjectStreamField;import java.io.IOException;/** * A <code>CardLayout</code> object is a layout manager for a * container. It treats each component in the container as a card. * Only one card is visible at a time, and the container acts as * a stack of cards. The first component added to a * <code>CardLayout</code> object is the visible component when the * container is first displayed. * <p> * The ordering of cards is determined by the container's own internal * ordering of its component objects. <code>CardLayout</code> * defines a set of methods that allow an application to flip * through these cards sequentially, or to show a specified card. * The {@link CardLayout#addLayoutComponent} * method can be used to associate a string identifier with a given card * for fast random access. * * @version 1.37 01/23/03 * @author Arthur van Hoff * @see java.awt.Container * @since JDK1.0 */public class CardLayout implements LayoutManager2, Serializable { private static final long serialVersionUID = -4328196481005934313L; /* * This creates a Vector to store associated * pairs of components and their names. * @see java.util.Vector */ Vector vector = new Vector(); /* * A pair of Component and String that represents its name. */ class Card implements Serializable { static final long serialVersionUID = 6640330810709497518L; public String name; public Component comp; public Card(String cardName, Component cardComponent) { name = cardName; comp = cardComponent; } } /* * Index of Component currently displayed by CardLayout. */ int currentCard = 0; /* * A cards horizontal Layout gap (inset). It specifies * the space between the left and right edges of a * container and the current component. * This should be a non negative Integer. * @see getHgap() * @see setHgap() */ int hgap; /* * A cards vertical Layout gap (inset). It specifies * the space between the top and bottom edges of a * container and the current component. * This should be a non negative Integer. * @see getVgap() * @see setVgap() */ int vgap; /** * @serialField tab Hashtable * deprectated, for forward compatibility only * @serialField hgap int * @serialField vgap int * @serialField vector Vector * @serialField currentCard int */ private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("tab", Hashtable.class), new ObjectStreamField("hgap", Integer.TYPE), new ObjectStreamField("vgap", Integer.TYPE), new ObjectStreamField("vector", Vector.class), new ObjectStreamField("currentCard", Integer.TYPE) }; /** * Creates a new card layout with gaps of size zero. */ public CardLayout() { this(0, 0); } /** * Creates a new card layout with the specified horizontal and * vertical gaps. The horizontal gaps are placed at the left and * right edges. The vertical gaps are placed at the top and bottom * edges. * @param hgap the horizontal gap. * @param vgap the vertical gap. */ public CardLayout(int hgap, int vgap) { this.hgap = hgap; this.vgap = vgap; } /** * Gets the horizontal gap between components. * @return the horizontal gap between components. * @see java.awt.CardLayout#setHgap(int) * @see java.awt.CardLayout#getVgap() * @since JDK1.1 */ public int getHgap() { return hgap; } /** * Sets the horizontal gap between components. * @param hgap the horizontal gap between components. * @see java.awt.CardLayout#getHgap() * @see java.awt.CardLayout#setVgap(int) * @since JDK1.1 */ public void setHgap(int hgap) { this.hgap = hgap; } /** * Gets the vertical gap between components. * @return the vertical gap between components. * @see java.awt.CardLayout#setVgap(int) * @see java.awt.CardLayout#getHgap() */ public int getVgap() { return vgap; } /** * Sets the vertical gap between components. * @param vgap the vertical gap between components. * @see java.awt.CardLayout#getVgap() * @see java.awt.CardLayout#setHgap(int) * @since JDK1.1 */ public void setVgap(int vgap) { this.vgap = vgap; } /** * Adds the specified component to this card layout's internal * table of names. The object specified by <code>constraints</code> * must be a string. The card layout stores this string as a key-value * pair that can be used for random access to a particular card. * By calling the <code>show</code> method, an application can * display the component with the specified name. * @param comp the component to be added. * @param constraints a tag that identifies a particular * card in the layout. * @see java.awt.CardLayout#show(java.awt.Container, java.lang.String) * @exception IllegalArgumentException if the constraint is not a string. */ public void addLayoutComponent(Component comp, Object constraints) { synchronized (comp.getTreeLock()) { if (constraints instanceof String) { addLayoutComponent((String)constraints, comp); } else { throw new IllegalArgumentException("cannot add to layout: constraint must be a string"); } } } /** * @deprecated replaced by * <code>addLayoutComponent(Component, Object)</code>. */ public void addLayoutComponent(String name, Component comp) { synchronized (comp.getTreeLock()) { if (!vector.isEmpty()) { comp.setVisible(false); } for (int i=0; i < vector.size(); i++) { if (((Card)vector.get(i)).name.equals(name)) { ((Card)vector.get(i)).comp = comp; return; } } vector.add(new Card(name, comp)); } } /** * Removes the specified component from the layout. * @param comp the component to be removed. * @see java.awt.Container#remove(java.awt.Component) * @see java.awt.Container#removeAll() */ public void removeLayoutComponent(Component comp) { synchronized (comp.getTreeLock()) { for (int i = 0; i < vector.size(); i++) { if (((Card)vector.get(i)).comp == comp) { // if we remove current component we should show next one if (comp.isVisible() && (comp.getParent() != null)) { next(comp.getParent()); } vector.remove(i); // correct currentCard if this is necessary if (currentCard > i) { currentCard--; } break; } } } } /** * Determines the preferred size of the container argument using * this card layout. * @param parent the name of the parent container. * @return the preferred dimensions to lay out the subcomponents * of the specified container. * @see java.awt.Container#getPreferredSize * @see java.awt.CardLayout#minimumLayoutSize */ public Dimension preferredLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); int ncomponents = parent.getComponentCount(); int w = 0; int h = 0; for (int i = 0 ; i < ncomponents ; i++) { Component comp = parent.getComponent(i); Dimension d = comp.getPreferredSize(); if (d.width > w) { w = d.width; } if (d.height > h) { h = d.height; } } return new Dimension(insets.left + insets.right + w + hgap*2, insets.top + insets.bottom + h + vgap*2); } } /** * Calculates the minimum size for the specified panel. * @param parent the name of the parent container * in which to do the layout. * @return the minimum dimensions required to lay out the * subcomponents of the specified container. * @see java.awt.Container#doLayout * @see java.awt.CardLayout#preferredLayoutSize */ public Dimension minimumLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); int ncomponents = parent.getComponentCount(); int w = 0; int h = 0; for (int i = 0 ; i < ncomponents ; i++) { Component comp = parent.getComponent(i); Dimension d = comp.getMinimumSize();
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -