⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 practical.html

📁 一些JAVA的小程序
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><head><title>How Do These Concepts Translate into Code?</title><script language="JavaScript"><!-- hidefunction openWin(term) {  url="../../information/glossary.html#" + term;  myWin= window.open(url, "Glossary",              "width=400,height=150,scrollbars=yes,status=no,toolbar=no,menubar=no");  myWin.focus();}//--></script><STYLE type="text/css"><!--.FigureCaption   { margin-left: 1in; margin-right: 1in; font-family: Arial, Helvetica, sans-serif; font-size: smaller; text-align: justify }--></STYLE> </head><body BGCOLOR="#ffffff" LINK="#000099"><B><FONT SIZE="-1">The Java</font><sup><font size="-2">TM</font></sup> <font size="-1">Tutorial</FONT></B><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>&gt;<a href="../index.html" target="_top">Start of Trail</a>&gt;<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><img src="../../images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" ALT=""><br><font size="-1"><b>Trail</b>: Learning the Java Language<br><b>Lesson</b>: Object-Oriented Programming Concepts</font><h2>How Do These Concepts Translate into Code?</h2><blockquote>Now that you have a conceptual understanding of object-oriented programminglet's look at how these concepts get translated into code.<span id="figure:concepts-ClickMeApp.gif">The following figure</span> shows a snapshot of the graphical user interface (GUI) presented by an example named <code>ClickMeApp</code>, which features a custom GUI component named <code>ClickMe</code>. A spot appears when you click the mouse inside the <code>ClickMe</code> component's area.<center><p><IMG SRC="../../figures/java/concepts-ClickMeApp.gif" WIDTH="220" HEIGHT="152" ALIGN="BOTTOM" ALT="The GUI of the ClickMeApp application."></p><p class="FigureCaption">The GUI of the <code>ClickMeApp</code> application.</p></center><p>The <code>ClickMeApp</code> example is not large, but if you don't have much experience with programming, you might find the code daunting. We don't expect you to understand everything in this example right away, and this section won't explain every detail. The intent is to expose you to some source code and to associate it with the concepts and terminology you've just learned. You will find more details in later chapters.</blockquote><h3>Compiling and Running the Example</h3><blockquote>There are three source files for the <code>ClickMeApp</code> example: <a target="_blank" href="ex5/ClickMeApp.java"><font color="#bb000f"><code>ClickMeApp.java</code></font></a><a target="_blank" href="ex5/ClickMeApp.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in a .java source file)"></a>,<a target="_blank" href="ex5/ClickMe.java"><font color="#bb000f"><code>ClickMe.java</code></font></a><a target="_blank" href="ex5/ClickMe.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in a .java source file)"></a>, and<a target="_blank" href="ex5/Spot.java"><font color="#bb000f"><code>Spot.java</code></font></a><a target="_blank" href="ex5/Spot.java"><img src="../../images/sourceIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in a .java source file)"></a>.These source files contain the code for three classes named <code>ClickMeApp</code>, <code>ClickMe</code>, and <code>Spot</code>. In addition to these classes, the example uses some classes provided by the Java platform.<P>You can compile the <code>ClickMeApp.java</code>, <code>ClickMe.java</code>, and <code>Spot.java</code> files by invoking the Java compiler (<code>javac</code>) on <code>ClickMeApp.java</code>. <P>You can run the <code>ClickMeApp</code> example as an application using either Java Web Start or the <code>java</code> command. To run a precompiled version with Java Web Start, direct your browser to this URL:<blockquote><pre><a target="_blank" href="http://java.sun.com/docs/books/tutorialJWS/java/concepts/ex5/ClickMeApp.jnlp"><font color="#009bbb">http://java.sun.com/docs/books/tutorialJWS/java/concepts/ex5/ClickMeApp.jnlp</font></a><a target="_blank" href="http://java.sun.com/docs/books/tutorialJWS/java/concepts/ex5/ClickMeApp.jnlp"><img src="../../images/otherIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (outside of the tutorial)"></a></pre></blockquote><p>If you prefer to run your own copy, you can use the command<code>java ClickMeApp</code>.<blockquote><hr><strong>Note:</strong>&nbsp;<code>ClickMeApp</code> depends on API introduced in J2SE 5.0. It will not run or compile under earlier versions of the JDK and runtime.<hr></blockquote>If you're having problems compiling or running this example, see<a target="_top" href="../../getStarted/problems/index.html">Common Problems (and Their Solutions)</a><a target="_top" href="../../getStarted/problems/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>Objects in the Example</h3><blockquote>Many objects play a part in the <code>ClickMeApp</code> example. The most obvious ones are those with an onscreen representation: the window containing the GUI, the label describing what the user should do, the custom component that paints the initially empty rectangle, and the spot that's eventually painted by that custom component.<P>These objects are created when the user launches the application. The application's <code>main</code> method creates an object to represent the entire application, and that object creates others to represent the window, label, and custom component.<P>The object representing the custom component in turn creates an object to represent the spot on the screen. Every time you click the mouse in the custom component, the component moves the spot by changing the spot object's <I>x</I>,<I>y</I> location and repainting itself. The spot object does not display itself; the custom component paints a circle based on information contained within the spot object. <P>Other, nonvisible objects also play a part in the application. Three objects represent the three colors used in the custom component (black, white, and olive green), an event object represents the user action of clicking the mouse, and so on.</blockquote><h3>Classes in the Example</h3><blockquote>As you may have guessed, the object that represents the application is an instance of the <code>ClickMeApp</code> class, the object that represents the custom component is an instance of <code>ClickMe</code>, and the spot is represented by an instance of <code>Spot</code>.<P>Because the object that represents the spot on the screen is very simple, let's look at its code. The <code>Spot</code> class declares three instance variables: <code>size</code> contains the spot's radius, <code>x</code> contains the spot's current horizontal location, and <code>y</code> contains the spot's current vertical location. It also declares two methods and a <I>constructor</I> &#151; a subroutine used to initialize new objects created from the class.<blockquote><pre>public class Spot {    //instance variables    public int x, y;    private int size;        //constructor    public Spot() {        x = -1;        y = -1;        size = 1;    }    //methods for access to the size instance variable        public void setSize(int newSize) {        if (newSize >= 0) {            size = newSize;        }    }     public int getSize() {        return size;    }}</pre></blockquote>You can recognize a constructor because it has the same name as the class. The <code>Spot</code> constructor initializes all three of the object's variables. The <code>x</code> and <code>y</code> variables are set to <code>-1</code>, indicating that the spot is not onscreen when the application starts up. The <code>size</code> variable is set to a reasonable value, in this case <code>1</code>.<P>The <code>setSize</code> and <code>getSize</code> methods provide a way for other objects to read and change the value of the size instance variable without giving other objects access to the actual variable.<P>The application's <code>ClickMe</code> object uses a <code>Spot</code> object to track whether to paint a spot, and where. Here's the <code>ClickMe</code> class code that declares and creates the <code>Spot</code> object. <blockquote><pre>private Spot spot = null;...spot = new Spot();</pre></blockquote>The first line shown declares a variable named spot with a data type of <code>Spot</code>; it initializes the variable's value to <code>null</code>. The keyword <code>null</code> is used to specify an undefined value for variables that refer to objects. The next line shown creates the object(see <span id="figure:concepts-spots.gif">the next figure</span>). The <code>new</code> keyword allocates memory space for the object. <code>Spot()</code> calls the constructor you saw previously. <center><p><IMG SRC="../../figures/java/concepts-spots.gif" WIDTH="508" HEIGHT="179" ALIGN="BOTTOM" ALT="In the object illustration, the x, y, and size variables have initial values."></p><p class="FigureCaption">On the left is a representation of the <code>Spot</code> class. On the right is a <code>Spot</code> object just after its creation. </p></center></blockquote><h3>Messages in the Example</h3><blockquote>As you know, object A can use a message to request that object B do something, and a message has three components:<ul><li> The object to which the message is addressed<li> The name of the method to perform<li> Any parameters the method needs</ul>Here are two lines of code from the <code>ClickMe</code> class.<blockquote><pre>g2d.setColor(Color.WHITE);g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);</pre></blockquote>Both are messages from the <code>ClickMe</code> object to an object named <code>g2d</code> &#151; a <code>Graphics2D</code> object that is essential for painting onscreen. This object is provided to the component when the painting system instructs the component to paint itself. The first line sets the color to white and the second fills a rectangle the size of the component, thus painting the extent of the component's area white.<P><span id="figure:concepts-messageParts.gif">The following figure</span> highlights each part of the first message to <code>g2d</code>.<p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -