[ Pobierz całość w formacie PDF ]
.drawString(theMessage, messageX, messageY);}public void mouseDragged(MouseEvent e) {// Save the mouse coordinates and paint the message.messageX = e.getX( );messageY = e.getY( );repaint( );}public void mouseMoved(MouseEvent e) {}public static void main(String[] args) {JFrame f = new JFrame("HelloJava2");// Make the application exit when the window is closed.f.addWindowListener(new WindowAdapter( ) {public void windowClosing(WindowEvent we) { System.exit(0); }});f.setSize(300, 300);f.getContentPane( ).add(new HelloJava2("Hello, Java!"));f.setVisible(true);}}Two slashes in a row indicates that the rest of the line is a comment.We've added a fewcomments to HelloJava2 to help you keep track of everything.Place the text of this example in a file called HelloJava2.java and compile it as before.You shouldget a new class file, HelloJava2.class, as a result.To run the new example, use the following command line:% java HelloJava2Feel free to substitute your own salacious comment for the "Hello, Java!" message, and enjoymany hours of fun, dragging the text around with your mouse.2.2.1 The import StatementSo, what have we added? First you may notice that a few lines are now hovering above our class:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class HelloJava2.The import statement lists external classes to use in this file and tells the compiler where to lookfor them.In our first example, we designated the JComponent class as the superclass ofHelloJava1.JComponent was not defined by us, and the compiler therefore had to look elsewhere for it.In that case, we referred to JComponent by its fully qualified name, which isjavax.swing.JComponent.The JComponent class and all the other classes in thejavax.swing package are stored in a standard location, known to the compiler.In this example, the statement import javax.swing.* enables us to refer to all the classes inthe javax.swing package by their simple names.For example, we don't have to use fullyqualified names to refer to the JComponent and JFrame classes.Our current example uses onlythe Graphics class from the java.awt package.So we could have used importjava.awt.Graphics instead of using the wildcard * to import all of the AWT package'sclasses.However, we are anticipating using several more classes from this package in theupcoming examples.We also import all the classes from the package java.awt.event ; these classes provide theEvent objects that we use to communicate with the user.By listening for events, we find outwhen the user moved the mouse, clicked a button, and so on.Notice that importing java.awt.*doesn't automatically import the event package.The asterisk imports only the classes in aparticular package, not other packages.Packages don't contain other packages, even if thehierarchical naming scheme would seem to imply such a thing.The import statement may seem a bit like the C or C++ preprocessor #include statement,which injects header files into programs at the appropriate places.This is not true; there are noheader files in Java.The import statement does not copy any code into a source file.It's just aconvenience.Think of it as "introducing" one or more external classes to the compiler; afterthey've been introduced, you can call them by their simple names, instead of by their fully-qualified names.2.2.2 Instance VariablesWe have added some variables to our example:int messageX = 125, messageY = 95;String theMessage;messageX and messageY are integers that hold the current coordinates of our movablemessage.They are initialized to default values, which should place the message somewherenear the center of the window.Java integers are always 32-bit signed numbers.There is nofretting about what architecture your code is running on; numeric types in Java are preciselydefined.The variable theMessage is of type String and can hold instances of the Stringclass.You should note that these three variables are declared inside the braces of the class definition,but not inside any particular method in that class.These variables are called instance variables ormember variables because they belong to the entire class, and copies of them appear in eachseparate instance of the class.Instance variables are always visible (usable) in any of themethods inside their class.Depending on their modifiers, they may also be accessible fromoutside the class.Unless otherwise initialized, instance variables are set to a default value of 0 (zero), false, ornull.Numeric types are set to zero, boolean variables are set to false, and class typevariables always have their value set to null, which means "no value." Attempting to use anobject with a null value results in a runtime error. Instance variables differ from method arguments and other variables that are declared inside of asingle method.The latter are called local variables.They are effectively private variables that canbe seen only by code inside the method.Java doesn't initialize local variables, so you mustassign values yourself.If you try to use a local variable that has not yet been assigned a value,your code will generate a compile-time error.Local variables live only as long as the method isexecuting and then disappear (which is fine, since nothing outside of the method can see themanyway).Each time the method is invoked, its local variables are recreated and must be assignedvalues.We have made some changes to our previously stodgy paintComponent( ) method.All of thearguments in the call to drawString( ) are now variables.2.2.3 ConstructorsThe HelloJava2 class includes a special kind of a method called a constructor [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •