📄 swing2.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>
Writing Advanced Java Applications, Chapter 6: Project Swing: Building a User Interface
</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)swing.src 1.3 07/20/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1">
<A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 6 Continued: Components and Data Models</FONT></H2>
<FONT SIZE="-1">[<A HREF="swing.html"><<BACK</A>]
[<A HREF="index.html#contents">CONTENTS</A>]
[<A HREF="render.html">NEXT>></A>]</FONT></DIV>
<P>
The <A HREF="./Code/admin/AuctionClient.java">AuctionClient</A> program
is a simple GUI application that lets auction administrators list and browse
auction items, and print auction item reports.
This section describes the Project Swing application code, which
uses lightweight components and the other Project Swing features
shown in the bullet list.
<P>
<IMG SRC="./Art/admin.gif" WIDTH="470" HEIGHT="219">
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#light">Lightweight Components</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#order">Ordering Components</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#model">Data Models</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#render">Custom Cell Rendering</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#edit">Custom Cell Editing</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#events">Specialized Event Handling</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#direction">Project Swing Directions</A></FONT>
</UL>
<HR>
<A NAME="light"></A>
<H3>Lightweight Components</H3>
All components in Project Swing, except <CODE>JApplet</CODE>, <CODE>
JDialog</CODE>, <CODE>JFrame</CODE> and <CODE>JWindow</CODE> are lightweight
components. Lightweight components, unlike their Abstract Window Toolkit (AWT)
counterparts, do not depend on the local windowing toolkit.
<P>
For example, a heavyweight <CODE>java.awt.Button</CODE> running on
the Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> platform for the Unix
platform maps to a real Motif button.
In this relationship, the Motif button is called the peer to the
<CODE>java.awt.Button</CODE>.
If you create two <CODE>java.awt.Button</CODE> in an application, two peers
and hence two Motif Buttons are also created. The Java platform
communicates with the Motif Buttons using the Java Native Interface.
For each and every component added to the application, there is an additional
overhead tied to the local windowing system, which is why these components
are called heavyweight.
<P>
Lightweight components are termed peerless components and emulate the local
window system components. A lightweight button is represented as a rectangle
with a label inside that accepts mouse events. Adding more lightweight
buttons means drawing more rectangles.
<P>
A lightweight component needs to be drawn on something, and an application
written in the Java programming language needs to interact with the local
window manager so the main application window can be closed or minimized.
This is why the top-level parent components mentioned above (<CODE>JFrame</CODE>,
<CODE>JApplet</CODE>, and others) are implemented as heavyweight
components -- they need to be mapped to a component in the local window
toolkit.
<P>
A <CODE>JButton</CODE> is a very simple shape to draw. For more complex
components like <CODE>JList</CODE> or <CODE>JTable</CODE>, the elements
or cells of the list or table are drawn by a <CODE>CellRenderer</CODE> object.
A <CODE>CellRenderer</CODE> object provides flexibility because it makes
it possible for any type of object to be displayed in any row or column.
<P>
For example, a <CODE>JTable</CODE> can use a different
<CODE>CellRenderer</CODE> for each column.
This code segment sets the second column, which is referenced as
index <CODE>1</CODE>, to use a <CODE>CustomRenderer</CODE> object
to create the cells for that column.
<PRE>
JTable scrollTable=new JTable(rm);
TableColumnModel scrollColumnModel =
scrollTable.getColumnModel();
CustomRenderer custom = new CustomRenderer();
scrollColumnModel.getColumn(1).setCellRenderer(custom);
</PRE>
<A NAME="order"></A>
<H3>Ordering Components</H3>
Each Project Swing applet or application needs at least one heavyweight
container component (a <CODE>JFrame</CODE>, <CODE>JWindow</CODE>,
<CODE>JApplet</CODE>, or <CODE>JDialog</CODE>). Each of these containers
with <CODE>JFrame's</CODE> lightweight MDI counterpart,
<CODE>JInternalFrame</CODE>, contains a component called a <CODE>RootPane</CODE>.
The <CODE>JRootPane</CODE> manages the additional layers
used in the container such as the <CODE>JLayeredPane</CODE>, <CODE>JContentPane</CODE>,
<CODE>GlassPane</CODE> and the optional <CODE>JMenuBar</CODE>. It also lets
all emulated (lightweight) components interact with the AWT event queue to
send and receive events. Interacting with the event queue gives emulated
components indirect interaction with the local window manager.
<H4>JLayeredPane</H4>
The <CODE>JLayeredPane</CODE> sits on top of the <CODE>JRootPane</CODE>,
and as its name implies, controls the layers of the components contained
within the boundary of the heavyweight container. The components are not
added to the <CODE>JLayeredPane</CODE>, but to the <CODE>JContentPane</CODE>
instead. The <CODE>JLayeredPane</CODE> determines the Z-ordering of the
components in the <CODE>JRootPane</CODE>. The Z-order can be thought of
as the order of overlay among various the components. If you drag-and-drop
a component or request a dialog to popup, you want that component to appear
in front of the others in the application window. The <CODE>JLayeredPane</CODE>
lets you layer components.
<P>
The <CODE>JLayeredPane</CODE> divides the depth of the container into
different bands that can be used to assign a component to a
type-appropriate level. The <CODE>DRAG_LAYER</CODE> band, value 400,
appears above all other defined component layters. The lowermost level
of <CODE>JLayeredpane</CODE>, the <CODE>DEFAULT_FRAME_LAYER</CODE> band,
has value -3000 and is the level of the heavyweight containers,
including the <CODE>MenuBar</CODE>. The bands are as follows:
<P>
<TABLE>
<TR>
<TH ALIGN="LEFT"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Value</FONT></TH>
<TH ALIGN="LEFT"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Layer Name</FONT></TH>
<TH ALIGN="LEFT"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Component Types</FONT></TH>
</TR>
<TR>
<TD WIDTH="75"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">-3000</FONT></TD>
<TD WIDTH="175"><CODE>DEFAULT_FRAME_LAYER</CODE></TD>
<TD><CODE>JMenubar</CODE></TD>
</TR>
<TR>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">0</FONT></TD>
<TD><CODE>DEFAULT_LAYER</CODE></TD>
<TD><CODE>JButton, JTable, ..</CODE></TD>
</TR>
<TR>
<TD></TD>
<TD><CODE>PALETTE_LAYER</CODE></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Floating components<BR>such as <CODE>JToolBar</CODE></FONT></TD>
</TR>
<TR>
<TD></TD>
<TD><CODE>MODAL_LAYER</CODE></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Modal Dialogs</FONT></TD>
</TR>
<TR>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">400</FONT></TD>
<TD><CODE>DRAG_LAYER</CODE></TD>
<TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Drag-and-drop<BR>over all layers</FONT></TD>
</TR>
</TABLE>
<P>
<IMG SRC="./Art/layer.gif" ALIGN="LEFT">
Within these general depth bands, components can be further arranged
with another numbering system to order the components in a
particular band, but this system reverses the numbering priority. For example,
in a specific band such as <CODE>DEFAULT_LAYER</CODE>, components with a value
of 0 appear in front of others in that band; whereas, components with a higher
number or -1 appear behind them. The highest number in
this scheme is the number of components minus 1, so one way to visualize
it is a vector of components that steps through painting the components
with a higher number first finishing with the one at position 0.
<P>
For example, the following code adds a <CODE>JButton</CODE> to the default
layer and specifies that it appear in front of the other components in that
same layer:
<PRE>
JButton enterButton = new JButton("Enter");
layeredPane.add(enterButton,
JLayeredPane.Default_Layer, 0);
</PRE>
You can achieve the same effect by calling the <CODE>LayeredPane.moveToFont</CODE>
method within a layer or using the <CODE>LayeredPane.setLayer</CODE> method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -