/* * @(#)CardTest.java 1.7 95/08/23 Arthur van Hoff * * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and * without fee is hereby granted. * Please refer to the file http://java.sun.com/copy_trademarks.html * for further important copyright and trademark information and to * http://java.sun.com/licensing.html for further important licensing * information for the Java (tm) Technology. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR * HIGH RISK ACTIVITIES. */ import java.awt.*; import java.applet.Applet; class CardPanel extends Panel { final static String one="Flow"; final static String two="Border"; final static String three="Grid"; final static String four="FlowLeft"; final static String five="Border2"; final static String six="Grid2"; Panel create(LayoutManager layout) { Panel p = new Panel(); p.setLayout(layout); //all LayoutManagers allow you to add parts //Some make use of North, Southwest,... p.add("North", whited(new Button(one))); p.add("West", whited(new Button(two))); p.add("South", whited(new Button(three))); p.add("East", whited(new Button(four))); p.add("Center", whited(new Button(five))); if( !(p.getLayout() instanceof BorderLayout)) p.add(whited(new Button(six))); return p; } Component whited(Component c){ c.setBackground(Color.white); return c;} CardPanel() { setLayout(new CardLayout());//Layout for this panel //In A Cardlayout only one component is visible at a time //as selected by the show(cardPanelObject, namePanel) add(one, create(new FlowLayout())); //In a FlowLaout items follow each other add(two, create(new BorderLayout())); //In a Borderlayout the Compus points position components add(three, create(new GridLayout(2, 2))); //In a Grid layou things are ina 2D array add(four, create(new FlowLayout(FlowLayout.LEFT, 10, 10))); //FlowLayout LEFT justified add(five, create(new BorderLayout(10, 10))); // border with bigger gaps in between add(six, create(new GridLayout(2, 2, 10, 10))); // grid with bigger gutters between //This does not show the more complex GridBagLayout } public Dimension preferredSize() { return new Dimension(200, 100); } }//CardPanel public class CardTest extends Applet { CardPanel cards; public CardTest() { setLayout(new BorderLayout());//Layout of this Applet! add("Center", cards = new CardPanel()); //card are put in the center of the Applet Panel p = new Panel();//will be for fixed buttons p.setLayout(new FlowLayout()); p.setBackground(Color.lightGray); add("South", p);//add panel p to Bottom of Applet p.add(new Button("first"));//Add button to panel on Applet p.add(new Button("next")); p.add(new Button("previous")); p.add(new Button("last")); Choice c = new Choice();//The button/menu at end of botom row. c.addItem(CardPanel.one);//item in choice c.addItem(CardPanel.two); c.addItem(CardPanel.three); c.addItem(CardPanel.four); c.addItem(CardPanel.five); c.addItem(CardPanel.six); //Choice c is complete so... p.add(c);//add it to the panel } /* action is called when something happens.... like a mouse click */ public boolean action(Event evt, Object arg) { if (evt.target instanceof Choice) { ((CardLayout)cards.getLayout()).setVisible( truecards,(String)arg); //asked layout manager for cards to show selected card. //Depends on the choices names match the card names! } else { if ("first".equals(arg)) { ((CardLayout)cards.getLayout()).first(cards); } else if ("next".equals(arg)) { ((CardLayout)cards.getLayout()).next(cards); } else if ("previous".equals(arg)) { ((CardLayout)cards.getLayout()).previous(cards); } else if ("last".equals(arg)) { ((CardLayout)cards.getLayout()).last(cards); } else { //it is some other button or thing ((CardLayout)cards.getLayout()).setVisible( truecards,(String)arg); } } return true;//this means that the action has been handled }//action public static void main(String args[]) { Frame f = new Frame("CardTest"); f.setBackground(Color.red); //f is where the Applet is put //construct the applet CardTest cardTest = new CardTest(); //constructor assmbled all the buttons and bows cardTest.init(); cardTest.start(); //and add it to the frame f.add("Center", cardTest); //make sure we have room f.setSize(400, 400); //display the whole thing f.setVisible( true); }//main }//CardTest