We've already seen how to create classes in a rudimentary way: You need to create a class to do anything at all, as when we created the main class for the applications we've built: public class ch10_01 { public static void main(String[] args) { System.out.println("Welcome to Java"); } } In preparation for the next chapter, let's take a look at a more advanced example. In this case, I'll create a new class named AppFrame based on the Java Frame class. This class is what you use to create frame windows (a frame window has a frame including a border and title bar) in Java. Here's what it will look like in the main method. I'll create a new object named f of the AppFrame class, passing the text we want to appear in that window to that class's constructor: public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); . . . } } Because the AppFrame class is built on the Java Frame class, I can use the Frame class's setSize method to give this new window a size of 400 x 200 pixels. I can use the show method to display it onscreen: public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.show(); } } Creating New ClassesThe AppFrame class is based on the Java Frame class, which means that AppFrame has all the built-in Frame class's methods. You can find those methods in Table 10-2. Table 10-2. Methods of the Frame Class
You can create your own classes with the class statement in Java, as we've seen. The AppFrame class is built on the Frame class, which in object-oriented terms means that AppFrame inherits the Frame class. To indicate that you want one class to be based on another, you use the extends keyword like this (note that I'm not using an access specifier when defining AppFrame , which means this class will use the default access specifier, which is private ): import java.awt.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { . . . } Note also that the Java Frame class is part of the Java Abstract Windowing Toolkit (AWT) package. That means that I must import this package with the statement import java.awt.*; . Creating a ConstructorThis new class needs a constructor because I want to pass the text that the window should display to that constructor. You create a constructor simply by creating a method in a class that has the same name as the class. In this case, that's AppFrame (constructors do not specify any return value): import java.awt.*; import java.awt.event.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { String displayText; public AppFrame(String text) { . . . } }
I'll store the text passed to the constructor in a string named displayText , this way: import java.awt.*; import java.awt.event.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { String displayText; public AppFrame(String text) { displayText = text; } } Using Graphics ObjectsThe next step is to display the text in the window itself. The Frame class has a method named paint that is automatically called whenever the window needs to be drawn on the screen. This method is passed an object of the Java Graphics class, which I will call g : import java.awt.*; import java.awt.event.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { String displayText; public AppFrame(String text) { displayText = text; } public void paint(Graphics g) { . . . } } You can use the Graphics object's methods to draw in a window, and you'll find a selection of those methods in Table 10-3. With the methods declared with the abstract keyword, you must base your own class on the Graphics class before using those methods (we won't use abstract methods in this book). Table 10-3. Methods of the Graphics Class
In this case, I'll use the drawString method to display the text in the window, like this: import java.awt.*; import java.awt.event.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { String displayText; public AppFrame(String text) { displayText = text; } public void paint(Graphics g) { g.drawString(displayText, 60, 100); } } Closing Application WindowsThere's one more refinement to make. When the user clicks the Close button at the upper right in the window we're creating, we'll need to handle the window-closing event that occurs. To handle events in Java, you use an event listener. In this case, I'll use an event listener to catch the window-closing event and, when that event occurs, end the program using the call System.exit(0); . This call ends the program and passes a value of (which indicates normal termination) as an exit code to the operating system. Handling Java events in detail is beyond the scope of this book, but here's how it works here. In this case, I'll add a window listener to the AppFrame object that will "listen" for window-closing events and, when one occurs, end the program. Note that to handle events with AWT objects, you must import the classes in the java.awt.event package: Listing ch10_14.javaimport java.awt.*; import java.awt.event.*; public class ch10_14 { public static void main(String argv[]) { AppFrame f = new AppFrame("Creating windowed Java applications..."); f.setSize(400, 200); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); f.show(); } } class AppFrame extends Frame { String displayText; public AppFrame(String text) { displayText = text; } public void paint(Graphics g) { g.drawString(displayText, 60, 100); } } You can see the results of this code in Figure 10-2. When you start this application, the window appears, displaying the text as shown. Figure 10-2. Running a windowed Java application.
Now that we're in the Java business, it's time to put all this technology to work with XML. I'll do that in the next chapter. |