150-155.html
来自「dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh」· HTML 代码 · 共 272 行
HTML
272 行
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:The Graphical User Interface</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=3//-->
<!--PAGES=150-155//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="146-150.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="155-160.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading33"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose that the following is predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
Frame f = new Frame(“Title of Frame”);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Frame</I> constructor is overloaded. You can either pass a string (which becomes the title of the frame) or you can leave the frame untitled:</P>
<!-- CODE SNIP //-->
<PRE>
Frame foo = new Frame();
Frame titledFrame = new Frame(“Here is a title”);
</PRE>
<!-- END CODE SNIP //-->
<P><I>Frame</I> instances always start life invisible and with a layout called <I>BorderLayout</I>. To get and set the title of a frame, use the following:</P>
<!-- CODE SNIP //-->
<PRE>
String title = f.getTitle();
f.setTitle(“My title”);
</PRE>
<!-- END CODE SNIP //-->
<P>To get and set the frame’s icon image:
</P>
<!-- CODE SNIP //-->
<PRE>
Image icon = f.getIconImage();
f.setIconImage(icon);
</PRE>
<!-- END CODE SNIP //-->
<P>To get and set the frame’s menu bar:
</P>
<!-- CODE SNIP //-->
<PRE>
MenuBar mb = f.getMenuBar();
f.setMenuBar(mb);
</PRE>
<!-- END CODE SNIP //-->
<P>To remove the menu bar:
</P>
<!-- CODE SNIP //-->
<PRE>
f.remove(m);
</PRE>
<!-- END CODE SNIP //-->
<P>To dispose of the frame:
</P>
<!-- CODE SNIP //-->
<PRE>
f.dispose;
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether the frame is resizable:
</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = f.isResizable();
</PRE>
<!-- END CODE SNIP //-->
<P>To set the resizablilty:
</P>
<!-- CODE SNIP //-->
<PRE>
f.setResizable(aBoolean);
</PRE>
<!-- END CODE SNIP //-->
<P>The cursor is set using constants that end with <I>_CURSOR</I>. To set the cursor to one of the preset cursors, use one of the following:</P>
<!-- CODE //-->
<PRE>
f.setCursor(DEFAULT_CURSOR);
f.setCursor(CROSSHAIR_CURSOR);
f.setCursor(TEXT_CURSOR);
f.setCursor(WAIT_CURSOR);
f.setCursor(SW_RESIZE_CURSOR);
f.setCursor(SE_RESIZE_CURSOR);
f.setCursor(NW_RESIZE_CURSOR);
f.setCursor(NE_RESIZE_CURSOR);
f.setCursor(N_RESIZE_CURSOR);
f.setCursor(S_RESIZE_CURSOR);
f.setCursor(W_RESIZE_CURSOR);
f.setCursor(E_RESIZE_CURSOR);
f.setCursor(HAND_CURSOR);
f.setCursor(MOVE_CURSOR);
</PRE>
<!-- END CODE //-->
<P>To get the cursor:
</P>
<!-- CODE SNIP //-->
<PRE>
anInt = f.getCursorType();
</PRE>
<!-- END CODE SNIP //-->
<P>It is typical to handle events within a frame. Speciality frames are often used to handle specific events. As an example, we show the <I>ClosableFrame</I>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading34"></A><FONT COLOR="#000077">The ClosableFrame Class</FONT></H4>
<P><I>ClosableFrame</I> is an extension of the <I>Frame</I> class that handles the close window event. It is often desirable to have windows that respond to the close event, and this event is typically handled with the following code fragment:</P>
<!-- CODE SNIP //-->
<PRE>
if(e.id == e.WINDOW_DESTROY) {
hide();
return true;
}
</PRE>
<!-- END CODE SNIP //-->
<P>Rather than repeat the same code in every event handler of every <I>Frame</I> instance, we have devised the <I>ClosableFrame</I> class:</P>
<!-- CODE //-->
<PRE>
package lyon.gui;
import java.awt.*;
public class ClosableFrame extends Frame {
// constructor needed to pass window title to class Frame
public ClosableFrame(String name) {
// call java.awt.Frame(String) constructor
super(name);
}
// needed to allow window close
public boolean handleEvent(Event e) {
// Window Destroy event
if (e.id == Event.WINDOW_DESTROY) {
dispose();
return true;
}
// it’s good form to let the super class look at any unhandled
events
return super.handleEvent(e);
} // end handleEvent()
} // end class ClosableFrame
</PRE>
<!-- END CODE //-->
<P>Any <I>Frame</I> that extends <I>ClosableFrame</I> will inherit the ability to handle the <I>WINDOW_DESTROY</I> event provided that the subclass returns super.<I>handleEvent</I>.</P>
<P>To take advantage of <I>ClosableFrame</I> we extend the it as follows:</P>
<!-- CODE SNIP //-->
<PRE>
...
public class AudioFrame extends ClosableFrame {
...
public AudioFrame(String name) {
super(name);
...
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>The constructor of the superclass is called with <I>super(name)</I> to ensure that the call to <I>java.awt.Frame(String)</I> will eventually be made.<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading35"></A><FONT COLOR="#000077">The Panel Class</FONT></H3>
<P>The <I>Panel</I> class is a generic extension of the <I>Container</I> class. It is often used to organize the layout of components into structured subparts, as in the example of the HTML Generator panel in Figure 3.9. The <I>Panel</I> instance has several components, including buttons, drop-down menus, and labels. These components are arranged to give the user a functional presentation of the input and output components. The <I>Panel</I> class represents a low-overhead container with its own layout manager instance. A <I>Panel</I> can also contain its own event handler, something that permits a more object-oriented approach to the event handling.</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/03-09.jpg',385,341 )"><IMG SRC="images/03-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-09.jpg',385,341)"><FONT COLOR="#000077"><B>Figure 3.9</B></FONT></A> The HTML Generator panel.</P>
<H4 ALIGN="LEFT"><A NAME="Heading36"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE SNIP //-->
<PRE>
public class Panel extends Container {
public Panel()
public synchronized void addNotify()
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading37"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>The default layout manager for the panel is <I>FlowLayout</I>. Suppose the following variable is predefined:</P>
<!-- CODE SNIP //-->
<PRE>
Panel p = new Panel();
</PRE>
<!-- END CODE SNIP //-->
<P>To create a peer for the <I>Panel</I> use the following:</P>
<!-- CODE SNIP //-->
<PRE>
p.addNotify();
</PRE>
<!-- END CODE SNIP //-->
<P>Beyond the <I>addNotify</I> and constructor (with default layout), the <I>Panel</I> inherits all its methods and properties from the <I>Container</I> class.</P>
<H4 ALIGN="LEFT"><A NAME="Heading38"></A><FONT COLOR="#000077">Building a Panel</FONT></H4>
<P>In the following code we see several components (some of which will be discussed later) being added to a panel.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>These components are in the <I>TargetControlPanel.java</I> file, a part of the HTML Generator interface<HR></FONT>
</BLOCKQUOTE>
<!-- CODE //-->
<PRE>
package htmlconverter;
import java.awt.*;
public class TargetControlPanel extends Panel {
Choice c;
TargetControlPanel() {
c = new Choice();
c.addItem(“Java”);
c.addItem(“C”);
c.addItem(“C++”);
c.select(“Java”);
setLayout(new GridLayout(1, 3, 10, 10));
add(new Label(“Target:”, Label.LEFT));
add(c);
}
}
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading39"></A><FONT COLOR="#000077">The Checkbox Class</FONT></H3>
<P>The <I>Checkbox</I> class extends the <I>Component</I> class. It is one of the many interface widgets that can be added to any <I>Container</I> subclass. Typically, a <I>Checkbox</I> instance is added to a <I>Panel</I> or a <I>Frame</I>. The state of the <I>Checkbox</I> instance becomes true if it is checked; otherwise, it is false. The nice thing about a <I>Checkbox</I> instance is that you do not have to handle the <I>Checkbox</I> event. <I>Checkbox</I> instances can be incorporated directly into Java programs.</P>
<P><I>Checkbox</I> instances can be grouped into another instance called a <I>CheckboxGroup</I>, commonly called a radio button. Radio buttons are a paradigm of the push-button station-selection feature available on some car radios. Users assign buttons to their favorite and then select a station with the push of the button. The basic rule of a radio button is that you can select only one button of the group. The act of selecting one button deselects the others and one button is always selected.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="146-150.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="155-160.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?