Java Reference
In-Depth Information
Using Card Layout in an Application
Today's next project demonstrates. both card layout and the use of different layout man-
agers within the same graphical user interface.
The SurveyWizard class is a panel that implements a wizard interface: a series of simple
questions accompanied by a Next button that is used to see the next question. The last
question has a Finish button instead.
Figure 11.5 shows this panel.
FIGURE 11.5
Using a card lay-
out for a wizard-
style interface.
The easiest way to implement a card-based layout is to use panels. The project uses pan-
els heavily:
The SurveyWizard class is a panel that holds all the cards.
n
11
The SurveyPanel helper class is a panel that holds one card.
n
Each SurveyPanel object contains three panels stacked on top of each other.
n
The SurveyWizard and SurveyPanel classes are both panels because that's the easiest
component to use when working with card layout. Each card is created as a panel and
added to a containing panel that will be used to show them in sequence.
This takes place in the SurveyWizard constructor, using two instance variables, a card
layout manager, and an array of three SurveyPanel objects:
SurveyPanel[] ask = new SurveyPanel[3];
CardLayout cards = new CardLayout();
The constructor sets the class to use the layout manager, creates each SurveyPanel
object, and then adds it to the class:
setLayout(cards);
String question1 = “What is your gender?”;
String[] responses1 = { “female”, “male”, “not telling” };
ask[0] = new SurveyPanel(question1, responses1, 2);
add(ask[0], “Card 0”);
Each SurveyPanel object is created with. three arguments to the constructor: the text of
the question, an array of possible responses, and the element number of the default
answer.
Search WWH ::




Custom Search