📄 tij0148.html
字号:
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>KeyListener</B></FONT><P><A NAME="Index1990"></A><A NAME="Index1991"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>KeyAdapter</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>keyPressed(KeyEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>keyReleased(KeyEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>keyTyped(KeyEvent)</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MouseListener</B></FONT><P><A NAME="Index1992"></A><A NAME="Index1993"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MouseAdapter</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseClicked(MouseEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseEntered(MouseEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseExited(MouseEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mousePressed(MouseEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseReleased(MouseEvent)</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MouseMotionListener</B></FONT><P><A NAME="Index1994"></A><A NAME="Index1995"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MouseMotionAdapter</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDragged(MouseEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseMoved(MouseEvent)</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowListener</B></FONT><P><A NAME="Index1996"></A><A NAME="Index1997"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowAdapter</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowOpened(WindowEvent)</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowClosing(WindowEvent)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowClosed(WindowEvent)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowActivated(WindowEvent)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowDeactivated(WindowEvent)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowIconified(WindowEvent)
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowDeiconified(WindowEvent)</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ItemListener</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>itemStateChanged(ItemEvent)</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextListener</B></FONT><P></DIV>
</TD>
<TD WIDTH=220 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>textValueChanged(TextEvent)</B></FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<A NAME="Heading426"></A><H4 ALIGN=LEFT>
Using
listener adapters for simplicity
<P><A NAME="Index1998"></A><A NAME="Index1999"></A><A NAME="Index2000"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the table above, you can see that some listener interfaces have only one
method. These are trivial to implement since you’ll implement them only
when you want to write that particular method. However, the listener interfaces
that have multiple methods could be less pleasant to use. For example,
something you must always do when creating an application is provide a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowListener</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so that when you get the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowClosing( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
event you can call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.exit(0)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to exit the application. But since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowListener</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you must implement all of the other methods even if they don’t do
anything. This can be annoying.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
solve the problem, each of the listener interfaces that have more than one
method are provided with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>adapters</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the names of which you can see in the table above. Each adapter provides
default methods for each of the interface methods. (Alas,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowAdapter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
have a default
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowClosing( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.exit(0)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.)
Then all you need to do is inherit from the adapter and override only the
methods you need to change. For example, the typical
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowListener</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you’ll use looks like this:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">class</font> MyWindowListener <font color="#0000ff">extends</font> WindowAdapter {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
System.exit(0);
}
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
whole point of the adapters is to make the creation of listener classes easy.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
is a downside to adapters, however, in the form of a pitfall. Suppose you write
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowAdapter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
like the one above:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">class</font> MyWindowListener <font color="#0000ff">extends</font> WindowAdapter {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> WindowClosing(WindowEvent e) {
System.exit(0);
}
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
doesn’t work, but it will drive you crazy trying to figure out why, since
everything will compile and run fine – except that closing the window
won’t exit the program. Can you see the problem? It’s in the name
of the method:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WindowClosing( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowClosing( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
A simple slip in capitalization results in the addition of a completely new
method. However, this is not the method that’s called when the window is
closing, so you don’t get the desired results.
</FONT><a name="_Toc408018708"></a><P></DIV>
<A NAME="Heading427"></A><H3 ALIGN=LEFT>
Making
windows and applets
<P>with
the Java 1.1 AWT
<P><A NAME="Index2001"></A><A NAME="Index2002"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Often
you’ll want to be able to create a class that can be invoked as either a
window or an applet. To accomplish this, you simply add a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to your applet that builds an instance of the applet inside a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
As a simple example, let’s look at
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Button2New.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
modified to work as both an application and an applet:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Button2NewB.java</font>
<font color="#009900">// An application and an applet</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*; <font color="#009900">// Must add this</font>
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Button2NewB <font color="#0000ff">extends</font> Applet {
Button
b1 = <font color="#0000ff">new</font> Button("Button 1"),
b2 = <font color="#0000ff">new</font> Button("Button 2");
TextField t = <font color="#0000ff">new</font> TextField(20);
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
b1.addActionListener(<font color="#0000ff">new</font> B1());
b2.addActionListener(<font color="#0000ff">new</font> B2());
add(b1);
add(b2);
add(t);
}
<font color="#0000ff">class</font> B1 <font color="#0000ff">implements</font> ActionListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
t.setText("Button 1");
}
}
<font color="#0000ff">class</font> B2 <font color="#0000ff">implements</font> ActionListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
t.setText("Button 2");
}
}
<font color="#009900">// To close the application:</font>
<font color="#0000ff">static</font> <font color="#0000ff">class</font> WL <font color="#0000ff">extends</font> WindowAdapter {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
System.exit(0);
}
}
<font color="#009900">// A main() for the application:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Button2NewB applet = <font color="#0000ff">new</font> Button2NewB();
Frame aFrame = <font color="#0000ff">new</font> Frame("Button2NewB");
aFrame.addWindowListener(<font color="#0000ff">new</font> WL());
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(<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">The
inner class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are the only two elements added to the applet, and the rest of the applet is
untouched. In fact, you can usually copy and paste the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>WL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -