📄 12_7_2.htm
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Weng Kai">
<meta name="GENERATOR" content="Mozilla/4.5 [en] (Win95; I) [Netscape]">
<title>Dialog boxes</title>
</head>
<body>
<h3>
12.7.2 Dialog boxes</h3>
<hr WIDTH="100%">
<br>A dialog box is a window that pops up out of another window. Its purpose
is to deal with some specific issue without cluttering the original window
with those details. Dialog boxes are heavily used in windowed programming
environments, but as mentioned previously, rarely used in applets.
<p>To create a dialog box, you inherit from <b>Dialog</b>, which is just
another kind of <b>Window</b>, like a <b>Frame</b>. Unlike a <b>Frame</b>,
a <b>Dialog</b> cannot have a menu bar or change the cursor, but other
than that they're quite similar. A dialog has a layout manager (which defaults
to <b>BorderLayout</b>) and you override <b>action( )</b> etc., or <b>handleEvent(
)</b> to deal with events. One significant difference you'll want to note
in <b>handleEvent( )</b>: when the <b>WINDOW_DESTROY</b> event occurs,
you don't want to shut down the application! Instead, you release the resources
used by the dialog's window by calling <b>dispose( )</b>.
<p>In the following example, the dialog box is made up of a grid (using
<b>GridLayout</b>)
of a special kind of button that is defined here as class <b>ToeButton</b>.
This button draws a frame around itself and, depending on its state, a
blank, an 'x', or an 'o' in the middle. It starts out blank, and then depending
on whose turn it is, changes to an 'x' or an 'o'. However, it will also
flip back and forth between 'x' and 'o' when you click on the button. (This
makes the tic-tac-toe concept only slightly more annoying than it already
is.) In addition, the dialog box can be set up for any number of rows and
columns by changing numbers in the main application window.
<p>Case Study: <a href="case/Windows/ToeTest.java">ToeTest.java</a>
<p>The ToeButton class keeps a handle to its parent, which must be of type
ToeDialog. As before, this introduces high coupling because a ToeButton
can be used only with a ToeDialog, but it solves a number of problems,
and in truth it doesn't seem like such a bad solution because there's no
other kind of dialog that's keeping track of whose turn it is. Of course,
you can take another approach, which is to make ToeDialog.turn a static
member of ToeButton. This eliminates the coupling, but prevents you from
having more than one ToeDialog at a time. (More than one that works properly,
anyway.)
<p>The paint( ) method is concerned with the graphics: drawing the square
around the button and drawing the 'x' or the 'o'. This is full of tedious
calculations, but it's straightforward. A mouse click is captured by the
overridden mouseDown( ) method, which first checks to see if the button
has anything written on it. If not, the parent window is queried to find
out whose turn it is and that is used to establish the state of the button.
Note that the button then reaches back into the parent and changes the
turn. If the button is already displaying an 'x' or an 'o' then that is
flopped. You can see in these calculations the convenient use of the ternary
if-else described in Chapter 3. After a button state change, the button
is repainted.
<p>The constructor for ToeDialog is quite simple: it adds into a GridLayout
as many buttons as you request, then resizes it for 50 pixels on a side
for each button. (If you don't resize a Window, it won't show up!) Note
that handleEvent( ) just calls dispose( ) for a WINDOW_DESTROY so the whole
application doesn't go away.
<p>ToeTest sets up the whole application by creating the TextFields (for
inputting the rows and columns of the button grid) and the "go" button.
You'll see in action( ) that this program uses the less-desirable "string
match" technique for detecting the button press (make sure you get spelling
and capitalization right!). When the button is pressed, the data in the
TextFields must be fetched, and, since they are in String form, turned
into ints using the static Integer.parseInt( ) method. Once the Dialog
is created, the show( ) method must be called to display and activate it.
<p>You'll notice that the ToeDialog object is assigned to a Dialog handle
d. This is an example of upcasting, although it really doesn't make much
difference here since all that's happening is the show( ) method is called.
However, if you wanted to call some method that existed only in ToeDialog
you would want to assign to a ToeDialog handle and not lose the information
in an upcast.
<p>
<hr WIDTH="100%">
<div align=right><a href="12_7_3.htm">Next Page</a></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -