📄 tij0147.html
字号:
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog.turn</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This eliminates the coupling, but prevents you from having more than one
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
at a time. (More than one that works properly, anyway.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1790"></A><A NAME="Index1791"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is concerned with the <A NAME="Index1792"></A><A NAME="Index1793"></A>graphics:
drawing the square around the button and drawing the “x” or the
“o.” This is full of tedious calculations, but it’s
straightforward.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
mouse click is captured by the overridden <A NAME="Index1794"></A><A NAME="Index1795"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDown( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
constructor for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is quite simple: it adds into a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GridLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as many buttons as you request, then resizes it for 50 pixels on a side for
each button. (If you don’t resize a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Window</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it won’t show up!) Note that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
just calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>dispose( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WINDOW_DESTROY</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so the whole application doesn’t go away.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeTest</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
sets up the whole application by creating the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
(for inputting the rows and columns of the button grid) and the
“go” button. You’ll see in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
must be fetched, and, since they are in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
form, turned into
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
<A NAME="Index1796"></A><A NAME="Index1797"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer.parseInt( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method. Once the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>show( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method must be called to display and activate it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You’ll
notice that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is assigned to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>d</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is an example of upcasting, although it really doesn’t make much
difference here since all that’s happening is the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>show( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is called. However, if you wanted to call some method that existed only
in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you would want to assign to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToeDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle and not lose the information in an upcast.
</FONT><P></DIV>
<A NAME="Heading422"></A><H4 ALIGN=LEFT>
File
dialogs
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Some
operating systems have a number of special built-in dialog boxes to handle the
selection of things such as fonts, colors, printers, and the like. Virtually
all graphical operating systems support the opening and saving of files,
however, and so Java’s <A NAME="Index1798"></A><A NAME="Index1799"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
encapsulates these for easy use. This, of course, makes no sense at all to use
from an applet since an applet can neither read nor write files on the local
disk. (This will change for trusted applets in newer browsers.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following application exercises the two forms of file dialogs, one for opening
and one for saving. Most of the code should by now be familiar, and all the
interesting activities happen in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the two different button clicks:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: FileDialogTest.java</font>
<font color="#009900">// Demonstration of File dialog boxes</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> FileDialogTest <font color="#0000ff">extends</font> Frame {
TextField filename = <font color="#0000ff">new</font> TextField();
TextField directory = <font color="#0000ff">new</font> TextField();
Button open = <font color="#0000ff">new</font> Button("Open");
Button save = <font color="#0000ff">new</font> Button("Save");
<font color="#0000ff">public</font> FileDialogTest() {
setTitle("File Dialog Test");
Panel p = <font color="#0000ff">new</font> Panel();
p.setLayout(<font color="#0000ff">new</font> FlowLayout());
p.add(open);
p.add(save);
add("South", p);
directory.setEditable(<font color="#0000ff">false</font>);
filename.setEditable(<font color="#0000ff">false</font>);
p = <font color="#0000ff">new</font> Panel();
p.setLayout(<font color="#0000ff">new</font> GridLayout(2,1));
p.add(filename);
p.add(directory);
add("North", p);
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> handleEvent(Event evt) {
<font color="#0000ff">if</font>(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.handleEvent(evt);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action(Event evt, Object arg) {
<font color="#0000ff">if</font>(evt.target.equals(open)) {
<font color="#009900">// Two arguments, defaults to open file:</font>
FileDialog d = <font color="#0000ff">new</font> FileDialog(<font color="#0000ff">this</font>,
"What file <font color="#0000ff">do</font> you want to open?");
d.setFile("*.java"); <font color="#009900">// Filename filter</font>
d.setDirectory("."); <font color="#009900">// Current directory</font>
d.show();
String openFile;
<font color="#0000ff">if</font>((openFile = d.getFile()) != <font color="#0000ff">null</font>) {
filename.setText(openFile);
directory.setText(d.getDirectory());
} <font color="#0000ff">else</font> {
filename.setText("You pressed cancel");
directory.setText("");
}
}
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(evt.target.equals(save)) {
FileDialog d = <font color="#0000ff">new</font> FileDialog(<font color="#0000ff">this</font>,
"What file <font color="#0000ff">do</font> you want to save?",
FileDialog.SAVE);
d.setFile("*.java");
d.setDirectory(".");
d.show();
String saveFile;
<font color="#0000ff">if</font>((saveFile = d.getFile()) != <font color="#0000ff">null</font>) {
filename.setText(saveFile);
directory.setText(d.getDirectory());
} <font color="#0000ff">else</font> {
filename.setText("You pressed cancel");
directory.setText("");
}
}
<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="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Frame f = <font color="#0000ff">new</font> FileDialogTest();
f.resize(250,110);
f.show();
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
an “open file” dialog, you use the constructor that takes two
arguments; the first is the parent window handle and the second is the title
for the title bar of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The method <A NAME="Index1800"></A><A NAME="Index1801"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setFile( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
provides an initial file name – presumably the native OS supports
wildcards, so in this example all the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files will initially be displayed. The <A NAME="Index1802"></A><A NAME="Index1803"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setDirectory( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method chooses the directory where the file selection will begin. (In general,
the OS allows the user to change directories.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1804"></A><A NAME="Index1805"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>show( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
command doesn’t return until the dialog is closed. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object still exists, so you can read data from it. If you call <A NAME="Index1806"></A><A NAME="Index1807"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getFile( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and it returns
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
it means the user canceled out of the dialog. Both the file name and the
results of <A NAME="Index1808"></A><A NAME="Index1809"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getDirectory( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are displayed in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
button for saving works the same way, except that it uses a different
constructor for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This constructor takes three arguments and the third argument must be either
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog.SAVE</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileDialog.OPEN</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc408018705"></a><a name="_Toc375545469"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0146.html">Prev</a> | <a href="tij0148.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -