📄 tij0144.html
字号:
application. This is true, but if you create more <A NAME="Index1710"></A><A NAME="Index1711"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Panel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, each one of those
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Panel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
can have its own layout manager and then be integrated into the applet or
application as simply another component, using the applet or
application’s layout manager. This gives you much greater flexibility as
seen in the following example:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: CardLayout1.java</font>
<font color="#009900">// Demonstrating the CardLayout</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.applet.Applet;
<font color="#0000ff">class</font> ButtonPanel <font color="#0000ff">extends</font> Panel {
ButtonPanel(String id) {
setLayout(<font color="#0000ff">new</font> BorderLayout());
add("Center", <font color="#0000ff">new</font> Button(id));
}
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> CardLayout1 <font color="#0000ff">extends</font> Applet {
Button
first = <font color="#0000ff">new</font> Button("First"),
second = <font color="#0000ff">new</font> Button("Second"),
third = <font color="#0000ff">new</font> Button("Third");
Panel cards = <font color="#0000ff">new</font> Panel();
CardLayout cl = <font color="#0000ff">new</font> CardLayout();
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
setLayout(<font color="#0000ff">new</font> BorderLayout());
Panel p = <font color="#0000ff">new</font> Panel();
p.setLayout(<font color="#0000ff">new</font> FlowLayout());
p.add(first);
p.add(second);
p.add(third);
add("North", p);
cards.setLayout(cl);
cards.add("First card",
<font color="#0000ff">new</font> ButtonPanel("The first one"));
cards.add("Second card",
<font color="#0000ff">new</font> ButtonPanel("The second one"));
cards.add("Third card",
<font color="#0000ff">new</font> ButtonPanel("The third one"));
add("Center", cards);
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action(Event evt, Object arg) {
<font color="#0000ff">if</font> (evt.target.equals(first)) {
cl.first(cards);
}
<font color="#0000ff">else</font> <font color="#0000ff">if</font> (evt.target.equals(second)) {
cl.first(cards);
cl.next(cards);
}
<font color="#0000ff">else</font> <font color="#0000ff">if</font> (evt.target.equals(third)) {
cl.last(cards);
}
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example begins by creating a new kind of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Panel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ButtonPanel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This contains a single button, placed at the center of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BorderLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which means that it will expand to fill the entire panel. The label on the
button will let you know which panel you’re on in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CardLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the applet, both the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Panel
cards
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
where the cards will live and the layout manager
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>cl</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CardLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must be members of the class because you need to have access to those handles
when you want to manipulate the cards.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
applet is changed to use a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BorderLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of its default
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FlowLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Panel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created to hold three buttons (using a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FlowLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
and this panel is placed at the “North” end of the applet. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>cards</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
panel is added to the “Center” of the applet, effectively occupying
the rest of the real estate.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you add the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ButtonPanel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
(or whatever other components you want) to the panel of cards, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method’s first argument is not “North,” “South,”
etc. Instead, it’s a string that describes the card. Although this string
doesn’t show up anywhere on the card, you can use it if you want to flip
that card using the string. This approach is not used in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">;
instead the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>first( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>next( ),</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>last( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods are used. Check your documentation for the other approach.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Java, the use of some sort of “tabbed panel” mechanism is quite
important because (as you’ll see later) in applet programming the use of
pop-up dialogs is heavily discouraged. For Java 1.0<A NAME="Index1712"></A>
applets, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CardLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the only viable way for the applet to have a number of different forms that
“pop up” on command.
</FONT><a name="_Toc375545462"></a><a name="_Toc408018698"></a><P></DIV>
<A NAME="Heading415"></A><H3 ALIGN=LEFT>
GridBagLayout</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Some
time ago, it was believed that all the stars, planets, the sun, and the moon
revolved around the earth. It seemed intuitive from observation. But then
astronomers became more sophisticated and started tracking the motion of
individual objects, some of which seemed at times to go backward in their
paths. Since it was known that everything revolved around the earth, those
astronomers spent large amounts of time coming up with equations and theories
to explain the motion of the stellar objects.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
trying to work with <A NAME="Index1713"></A><A NAME="Index1714"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">you
can consider yourself the analog of one of those early astronomers. The basic
precept (decreed, interestingly enough, by the designers at “Sun”)
is that everything should be done in code. The Copernican revolution (again
dripping with irony, the discovery that the planets in the solar system revolve
around the sun) is the use of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>resources</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to determine the layout and make the programmer’s job easy. Until these
are added to Java, you’re stuck (to continue the metaphor) in the Spanish
Inquisition of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagConstraints</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">My
recommendation is to avoid
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Instead, use the other layout managers and especially the technique of
combining several panels using different layout managers within a single
program. Your applets won’t look
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>that</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
different; at least not enough to justify the trouble that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
entails. For my part, it’s just too painful to come up with an example
for this (and I wouldn’t want to encourage this kind of library design).
Instead, I’ll refer you to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Core
Java
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by Cornell & Horstmann (2
</FONT><SUP><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">nd</FONT></SUP><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
ed., Prentice-Hall, 1997) to get started.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There’s
another light on the horizon: in the JFC/Swing library there is a new layout
manager that uses Smalltalk’s popular “Springs and Struts,”
and this could significantly reduce the need for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridBagLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545463"></a><a name="_Toc408018699"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0143.html">Prev</a> | <a href="tij0145.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -