Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/CardTest/CardTest.java
38827 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.applet.Applet;42import java.awt.BorderLayout;43import java.awt.Button;44import java.awt.CardLayout;45import java.awt.Choice;46import java.awt.Dimension;47import java.awt.FlowLayout;48import java.awt.Frame;49import java.awt.GridLayout;50import java.awt.LayoutManager;51import java.awt.Panel;52import java.awt.event.ActionEvent;53import java.awt.event.ActionListener;54import java.awt.event.ItemEvent;55import java.awt.event.ItemListener;565758@SuppressWarnings("serial")59final class CardPanel extends Panel {6061ActionListener listener;6263Panel create(LayoutManager layout) {64Button b = null;65Panel p = new Panel();6667p.setLayout(layout);6869b = new Button("one");70b.addActionListener(listener);71p.add("North", b);7273b = new Button("two");74b.addActionListener(listener);75p.add("West", b);7677b = new Button("three");78b.addActionListener(listener);79p.add("South", b);8081b = new Button("four");82b.addActionListener(listener);83p.add("East", b);8485b = new Button("five");86b.addActionListener(listener);87p.add("Center", b);8889b = new Button("six");90b.addActionListener(listener);91p.add("Center", b);9293return p;94}9596CardPanel(ActionListener actionListener) {97listener = actionListener;98setLayout(new CardLayout());99add("one", create(new FlowLayout()));100add("two", create(new BorderLayout()));101add("three", create(new GridLayout(2, 2)));102add("four", create(new BorderLayout(10, 10)));103add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));104add("six", create(new GridLayout(2, 2, 10, 10)));105}106107@Override108public Dimension getPreferredSize() {109return new Dimension(200, 100);110}111}112113114@SuppressWarnings("serial")115public class CardTest extends Applet116implements ActionListener,117ItemListener {118119CardPanel cards;120121@SuppressWarnings("LeakingThisInConstructor")122public CardTest() {123setLayout(new BorderLayout());124add("Center", cards = new CardPanel(this));125Panel p = new Panel();126p.setLayout(new FlowLayout());127add("South", p);128129Button b = new Button("first");130b.addActionListener(this);131p.add(b);132133b = new Button("next");134b.addActionListener(this);135p.add(b);136137b = new Button("previous");138b.addActionListener(this);139p.add(b);140141b = new Button("last");142b.addActionListener(this);143p.add(b);144145Choice c = new Choice();146c.addItem("one");147c.addItem("two");148c.addItem("three");149c.addItem("four");150c.addItem("five");151c.addItem("six");152c.addItemListener(this);153p.add(c);154}155156@Override157public void itemStateChanged(ItemEvent e) {158((CardLayout) cards.getLayout()).show(cards,159(String) (e.getItem()));160}161162@Override163public void actionPerformed(ActionEvent e) {164String arg = e.getActionCommand();165166if ("first".equals(arg)) {167((CardLayout) cards.getLayout()).first(cards);168} else if ("next".equals(arg)) {169((CardLayout) cards.getLayout()).next(cards);170} else if ("previous".equals(arg)) {171((CardLayout) cards.getLayout()).previous(cards);172} else if ("last".equals(arg)) {173((CardLayout) cards.getLayout()).last(cards);174} else {175((CardLayout) cards.getLayout()).show(cards, arg);176}177}178179public static void main(String args[]) {180Frame f = new Frame("CardTest");181CardTest cardTest = new CardTest();182cardTest.init();183cardTest.start();184185f.add("Center", cardTest);186f.setSize(300, 300);187f.setVisible(true);188}189190@Override191public String getAppletInfo() {192return "Demonstrates the different types of layout managers.";193}194}195196197