📄 tij316.htm
字号:
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This is a tool you may want to use yourself, so it’s placed in the library <a name="Index1693"></a><b>com.bruceeckel.swing</b>. The <b>Console</b> class consists entirely of <b>static</b> methods. The first is used to extract the class name (using RTTI) from any object and to remove the word “class,” which is typically prepended by <b>getClass( )</b>. This uses the <b>String</b> methods <b>indexOf( )</b> to determine whether the word “class” is there, and <b>substring( )</b> to produce the new string without “class” or the trailing space. This name is used to label the window that is displayed by the <b>run( )</b> methods. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1944" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>setDefaultCloseOperation( )</b> causes a <b>JFrame</b> to exit a program when that <b>JFrame</b> is closed. The default behavior is to do nothing, so if you don’t call <b>setDefaultCloseOperation( )</b> or write the equivalent code for your <b>JFrame</b><a name="Index1694"></a>, the application won’t close. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1945" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The <b>run( )</b> method is overloaded to work with <b>JApplet</b>s, <b>JPanel</b>s, and <b>JFrame</b>s. Note that only if it’s a <b>JApplet</b> are <b>init( )</b> and <b>start( )</b> called. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1946" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Now any applet can be run from the console by creating a <b>main( )</b> containing a line like this:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Console.run(<font color=#0000ff>new</font> MyClass(), 500, 300);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>in which the last two arguments are the display width and height. Here’s <b>Applet1c.java</b> modified to use <b>Console</b>:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Applet1d.java</font>
<font color=#009900>// Console runs applets from the command line.</font>
<font color=#009900>// <applet code=Applet1d width=100 height=50></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Applet1d <font color=#0000ff>extends</font> JApplet {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
getContentPane().add(<font color=#0000ff>new</font> JLabel(<font color=#004488>"Applet!"</font>));
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Console.run(<font color=#0000ff>new</font> Applet1d(), 100, 50);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This allows the elimination of repeated code while providing the greatest flexibility in running the examples. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1947" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545447"></a><a name="_Toc24775872"></a><a name="Heading18699"></a>Making
a button<br></h2>
<p><a name="Index1695"></a><a name="Index1696"></a>Making a button is quite simple: you just call the <b>JButton</b> constructor with the label you want on the button. You’ll see later that you can do fancier things, like putting graphic images on buttons. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1952" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Usually, you’ll want to create a field for the button inside your class so that you can refer to it later. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1953" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The <b>JButton</b> is a component—its own little window—that will automatically get repainted as part of an update. This means that you don’t explicitly paint a button or any other kind of control; you simply place them on the form and let them automatically take care of painting themselves. So to place a button on a form, you do it inside <b>init( )</b>:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Button1.java</font>
<font color=#009900>// Putting buttons on an applet.</font>
<font color=#009900>// <applet code=Button1 width=200 height=50></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Button1 <font color=#0000ff>extends</font> JApplet {
<font color=#0000ff>private</font> JButton
b1 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 1"</font>),
b2 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 2"</font>);
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
Container cp = getContentPane();
cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
cp.add(b1);
cp.add(b2);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Console.run(<font color=#0000ff>new</font> Button1(), 200, 50);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Something new has been added here: Before any elements are placed on the content pane, it is given a new “layout manager,” of type <b>FlowLayout</b>. The layout manager is the way that the pane implicitly decides where to place the control on the form. The normal behavior of an applet is to use the <b>BorderLayout</b>, but that won’t work here because (as you will learn later in this chapter when controlling the layout of a form is examined in more detail) it defaults to covering each control entirely with every new one that is added. However, <b>FlowLayout</b> causes the controls to flow evenly onto the form, left to right and top to bottom. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1954" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545448"></a><a name="_Toc24775873"></a><a name="Heading18726"></a>Capturing
an event<br></h2>
<p><a name="Index1697"></a>You’ll notice that if you compile and run the preceding applet, nothing happens when you press the buttons. This is where you must step in and write some code to determine what will happen. The basis of event-driven programming, which comprises a lot of what a GUI is about, is tying events to code that responds to those events. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1955" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1698"></a><a name="Index1699"></a>The way that this is accomplished in Swing is by cleanly separating the interface (the graphical components) and the implementation (the code that you want to run when an event happens to a component). Each Swing component can report all the events that might happen to it, and it can report each kind of event individually. So if you’re not interested in, for example, whether the mouse is being moved over your button, you don’t register your interest in that event. It’s a very straightforward and elegant way to handle event-driven programming, and once you understand the basic concepts, you can easily use Swing components that you haven’t seen before—in fact, this model extends to anything that can be classified as a JavaBean (discussed later in the chapter). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1956" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>At first, we will just focus on the main event of interest for the components being used. In the case of a <b>JButton</b>, this “event of interest” is that the button is pressed.<b> </b>To register your interest in when a button is pressed, you call the <b>JButton</b>’s <b>addActionListener( )</b> method. This method expects an argument that is an object that implements the <b>ActionListener</b> interface, which contains a single method called <b>actionPerformed( )</b>. So all you have to do to attach code to a <b>JButton</b> is to implement the <b>ActionListener</b> interface in a class, and register an object of that class with the <b>JButton</b> via <b>addActionListener( )</b>. The method will be called when the button is pressed (this is normally referred to as a <a name="Index1700"></a><i>callback</i>). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1957" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>But what should the result of pressing that button be? We’d like to see something change on the screen, so a new Swing component will be introduced: the <a name="Index1701"></a><b>JTextField</b>. This is a place where text can be typed, or in this case, inserted by the program. Although there are a number of ways to create a <b>JTextField</b>, the simplest is just to tell the constructor how wide you want that field to be. Once the <b>JTextField</b> is placed on the form, you can modify its contents by using the <b>setText( )</b> method (there are many other methods in <b>JTextField</b>, but you must look these up in the HTML documentation for the JDK from <i>java.sun.com</i>). Here is what it looks like:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Button2.java</font>
<font color=#009900>// Responding to button presses.</font>
<font color=#009900>// <applet code=Button2 width=200 height=75></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Button2 <font color=#0000ff>extends</font> JApplet {
<font color=#0000ff>private</font> JButton
b1 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 1"</font>),
b2 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 2"</font>);
<font color=#0000ff>private</font> JTextField txt = <font color=#0000ff>new</font> JTextField(10);
<font color=#0000ff>class</font> ButtonListener <font color=#0000ff>implements</font> ActionListener {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
String name = ((JButton)e.getSource()).getText();
txt.setText(name);
}
}
<font color=#0000ff>private</font> ButtonListener bl = <font color=#0000ff>new</font> ButtonListener();
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
b1.addActionListener(bl);
b2.addActionListener(bl);
Container cp = getContentPane();
cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Console.run(<font color=#0000ff>new</font> Button2(), 200, 75);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Creating a <b>JTextField</b> and placing it on the canvas takes the same steps as for <b>Jbutton</b>s or for any Swing component. The difference in the preceding program is in the creation of the aforementioned <b>ActionListener</b> class <b>ButtonListener</b>. The argument to <b>actionPerformed( )</b> is of type <b>ActionEvent</b>, which contains all the information about the event and where it came from. In this case, I wanted to describe the button that was pressed; <b>getSource( )</b> produces the object where the event originated, and I assumed (using a cast) that the object is a <b>JButton</b>. <b>getText( )</b> returns the text that’s on the button, and this is placed in the <b>JTextField</b> to prove that the code was actually called when the button was pressed. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1958" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In <b>init( )</b>, <b>addActionListener( )</b> is used to register the <b>ButtonListener</b> object with both the buttons. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1959" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>It is often more convenient to code the <b>ActionListener</b> as an anonymous inner class, especially since you tend to use only a single instance of each listener class. <a name="Index1702"></a><a name="Index1703"></a><a name="Index1704"></a><b>Button2.java</b> can be modified to use an anonymous inner class as follows: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1960" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Button2b.java</font>
<font color=#009900>// Using anonymous inner classes.</font>
<font color=#009900>// <applet code=Button2b width=200 height=75></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Button2b <font color=#0000ff>extends</font> JApplet {
<font color=#0000ff>private</font> JButton
b1 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 1"</font>),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -