📄 practical.html
字号:
<center><p><IMG SRC="../../figures/java/concepts-messageParts.gif" WIDTH="420" HEIGHT="64" ALIGN="BOTTOM" ALT="g2d.setColor(Color.white), with the receiving object (g2d), method name (setColor), and parameters (Color.white) called out."></p><p class="FigureCaption">The components of a message sent by the <code>ClickMe</code> object.</p></center></blockquote><h3>Inheritance in the Example</h3><blockquote>To paint itself onscreen, an object must be a component. This means that the object must be an instance of a class that derives from the <code>Component</code> class provided by the Java platform. <p>The <code>ClickMe</code> object is an instanceof the <code>ClickMe</code> class, which is declared this way:<blockquote><pre>public class ClickMe<font color="#6b7402"><B> extends JComponent</B></font> implements MouseListener { ...}</pre></blockquote>The <code>extends JComponent</code> clause makes <code>ClickMe</code> a subclass of <code>JComponent</code> — a class that derives from the <code>Component</code> class. <code>ClickMe</code> inherits a lot of functionality from <code>Component</code> and <code>JComponent</code>, including the ability to paint itself, to have a standard border around its edges, and to register listeners to be notified of mouse events. Along with these benefits, the <code>ClickMe</code> class has certain obligations: Its painting code must be in a standard place,such as the <code>paintComponent</code> method; it mustspecify its preferred and minimum sizes; and so on.<blockquote><pre>public ClickMe() { ... setPreferredSize(<I>aDimensionObject</I>); setMinimumSize(<I>anotherDimensionObject</I>); ...}public void paintComponent(Graphics g) { ... //<I>ClickMe's painting code is here.</I>}</pre></blockquote></blockquote><h3>Interfaces in the Example</h3><blockquote>The <code>ClickMe</code> component responds to mouse clicks bydisplaying a spot at the click location.Any object can detect mouse clicks on a component. It just needs to implement the <code>MouseListener</code> interface and register with the component as a mouse listener.<P>The <code>MouseListener</code> interface declares five methods, each of which is called for a different kind of mouse event: when the mouse is clicked, when the mouse moves outside of the component, and so on. Even though the <code>ClickMe</code> component responds only to mouse clicks, its mouse listener implementation must have all five methods. The methods for the events that the object isn't interested in are empty.<P>The complete code for the <code>ClickMe</code> component follows. The code that participates in mouse event handling is in <font color="#6b7402" ><b>colored boldface</b></font>.<blockquote><pre>import javax.swing.BorderFactory;import javax.swing.JComponent;import java.awt.*;import java.awt.event.*;public class ClickMe extends JComponent <font color="#6b7402"><B> implements MouseListener</B></font> { private Spot spot = null; private static final int RADIUS = 7; private Color spotColor = new Color(107, 116, 2); //olive /** Creates and initializes the ClickMe component. */ public ClickMe() { <font color="#6b7402"><B> addMouseListener(this);</B></font> //Hint at good sizes for this component. setPreferredSize(new Dimension(RADIUS*30, RADIUS*15)); setMinimumSize(new Dimension(RADIUS*4, RADIUS*4)); //Request a black line around this component. setBorder( BorderFactory.createLineBorder(Color.BLACK)); } /** * Paints the ClickMe component. This method is * invoked by the Swing component-painting system. */ public void paintComponent(Graphics g) { /** * Copy the graphics context so we can change it. * Cast it to Graphics2D so we can use antialiasing. */ Graphics2D g2d = (Graphics2D)g.create(); //Turn on antialiasing so that painting is smooth. g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //Paint the background. g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1); //Paint the spot. if (spot != null) { int radius = spot.getSize(); g2d.setColor(spotColor); g2d.fillOval(spot.x - radius, spot.y - radius, radius * 2, radius * 2); } } <font color="#6b7402"><B>//Methods required by the MouseListener interface. public void mousePressed(MouseEvent event) { if (spot == null) { spot = new Spot(); spot.setSize(RADIUS); } spot.x = event.getX(); spot.y = event.getY(); repaint(); } public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {}</B></font>}</pre></blockquote></blockquote><h3>API Documentation</h3><blockquote>To learn more about how <code>ClickMe</code> works, you need to learn about its superclasses — <code>JComponent</code> and <code>Component</code>. How do you find that information? The API documentation, which constitutes the specification for the classes that make up the Java platform,contains detailed descriptions of every class.<P>The API documentation for the Java 2 Platform is online at <code>java.sun.com</code>. It's helpful to have the API documentation for all releases you use bookmarked in your browser. You can find the 5.0 API documentation here:<blockquote><pre><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/"><font color="#8800bb">http://java.sun.com/j2se/5.0/docs/api/</font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a></pre></blockquote><P>To learn more about all the classes and interfaces from theJava platform used by the <code>ClickMe</code> class,you can look at the API documentation for the following classes.<UL><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/javax/swing/JComponent.html"><font color="#8800bb"><code>javax.swing.JComponent</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/javax/swing/JComponent.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Graphics.html"><font color="#8800bb"><code>java.awt.Graphics</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Graphics.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Graphics2D.html"><font color="#8800bb"><code>java.awt.Graphics2D</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Graphics2D.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Dimension.html"><font color="#8800bb"><code>java.awt.Dimension</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Dimension.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Color.html"><font color="#8800bb"><code>java.awt.Color</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/Color.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/javax/swing/BorderFactory.html"><font color="#8800bb"><code>javax.swing.BorderFactory</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/javax/swing/BorderFactory.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/event/MouseListener.html"><font color="#8800bb"><code>java.awt.event.MouseListener</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/event/MouseListener.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a><LI><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/event/MouseEvent.html"><font color="#8800bb"><code>java.awt.event.MouseEvent</code></font></a><a target="_blank" href="http://java.sun.com/j2se/5.0/docs/api/java/awt/event/MouseEvent.html"><img src="../../images/apiIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the API reference documentation)"></a></UL>A complete discussion of usingthese classesthemto create GUIs is in<a target="_top" href="../../uiswing/index.html">Creating a GUI with JFC/Swing</a><a target="_top" href="../../uiswing/index.html"><img src="../../images/tutorialIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the Learning the Java Language trail)"></a>.</blockquote><h3>Summary</h3><blockquote>The discussion in this chapter glossed over many details and left some things unexplained, but now you should have some understanding of what object-oriented concepts look like in code. Specifically, you should have a general understanding of the following:<ul><li>That a class is a blueprint for objects<li>That objects are created from classes<li>How to create an object from a class<li>What constructors are<li>How to initialize objects<li>What the code for a class looks like<LI>What class variables and methods are<LI>What instance variables and methods are<LI>How to find out what a class's superclass is<li>That an interface is a protocol of behavior<LI>What it means to implement an interface</ul></blockquote></blockquote><img src="../../images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" ALT=""><br><table width="550" summary="layout"><tr><td align="left" valign="middle"><a href="interface.html" target="_top"><img src="../../images/PreviousArrow.gif" width="26" height="26" align="middle" border="0" alt="Previous Page"></a><a href="../TOC.html#concepts" target="_top"><img src="../../images/TOCIcon.gif" width="26" height="26" align="middle" border="0" alt="Lesson Contents"></a><a href="QandE/questions.html" target="_top"><img src="../../images/NextArrow.gif" width="26" height="26" align="middle" border="0" alt="Next Page"></a></td><td align="center" valign="middle"><font size="-1"><a href="../../index.html" target="_top">Start of Tutorial</a>><a href="../index.html" target="_top">Start of Trail</a>><a href="index.html" target="_top">Start of Lesson</a></font></td><td align="right" valign="middle"><font size="-1"><a href="../../search.html" target="_top">Search</a><br><a href="http://developer.sun.com/contact/tutorial_feedback.jsp">Feedback Form</a></font></td></tr></table><p><font size="-1"><a href="../../information/copyright.html">Copyright</a>1995-2005 Sun Microsystems, Inc. All rights reserved.</font></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -