Making windows and patches with Java 1.1 AWT

Source: Internet
Author: User
Tags anonymous exit html page static class

We often need to create a class that can be called either as a window or as a piece of a program. To do this, simply add a main () to the program to create an instance of the program in a frame (frame). As a simple example, let's look at how to modify the Button2new.java so that it can be used as both an application and a piece of program:
 

: Button2newb.java//An application and a applet import java.awt.*; Import java.awt.event.*;

Must add this import java.applet.*;
  public class Button2newb extends Applet {button B1 = New button ("button 1"), B2 = New button ("button 2");
  TextField t = new TextField (20);
    public void init () {B1.addactionlistener (New B1 ());
    B2.addactionlistener (New B2 ());
    Add (B1);
    Add (B2);
  Add (t);
    Class B1 implements ActionListener {public void actionperformed (ActionEvent e) {t.settext ("button 1"); Class B2 implements ActionListener {public void actionperformed (ActionEvent e) {t.settext ("button 2"
    ); }//To close the Application:static class WL extends Windowadapter {public void windowclosing (WindowEvent E
    ) {system.exit (0); }//A main () for the application:public static void main (string[] args) {button2newb applet = new Button2ne
    WB ();
Frame aframe = new Frame ("BUTTON2NEWB");    Aframe.addwindowlistener (New WL ());
    Aframe.add (applet, borderlayout.center);
    Aframe.setsize (300,200);
    Applet.init ();
    Applet.start ();
  Aframe.setvisible (TRUE); }
} ///:~

The inner class WL and main () methods are the only two elements that add to the program slice, and the remainder of the patch remains intact. In fact, we usually make a small improvement to the WL class and the main () method to copy and paste into our own patches (remember that creating an inner class usually requires an external class to handle it, forming it to eliminate the need statically). We can see that in the main () method, the program is explicitly initialized and started, because in this case the browser cannot run it efficiently for us. Of course, this does not provide all the browser calls to stop () and destroy (), but it is acceptable in most cases. If it becomes a trouble, we can:
(1) Make the program piece handle a static class (in place of the locally variable Main ()), and then:
(2) Call Applet.stop () and Applet.destroy () in windowadapter.windowclosing () before we call System.exit ().
Note the last line:
Aframe.setvisible (TRUE);
This is a change in the Java 1.1 AWT. The show () method is no longer supported, and setvisible (TRUE) replaces the show () method. When we learn Java beans later in this chapter, these seemingly easy to change methods will become more reasonable.
This example is also used TextField to modify rather than display to the console or browser state line. One limiting condition in developing a program is that both the program and the application have to choose the input and output structure based on their operation.
This shows some of the other small new features of the Java 1.1 AWT. We no longer need to use the error-prone method of specifying borderlayout positioning using strings. When we add an element to the Java 1.1 version of BorderLayout, we can write this:
Aframe.add (applet, borderlayout.center);
We specify a BorderLayout constant for the position so that it can be tested at compile time (rather than quietly doing inappropriate things to the old structure). This is a significant improvement and will be used heavily in the remainder of this book.

2. Turn the window receiver into an anonymous class
Any of the receiver classes can be executed as an anonymous class, but it has been an accident that we may need to use their functionality on other occasions. However, the window sink is only used here as a closed application window, so we can safely create an anonymous class. Then, the following line of code in main ():
Aframe.addwindowlistener (New WL ());
will become:

Aframe.addwindowlistener (
  new Windowadapter () {public
    void windowclosing (WindowEvent e) {
      system.exit (0 );
    }
  });

One advantage of this is that it does not require other class names. We have to judge for ourselves whether it makes the code easier to understand or more difficult. However, for the remainder of this book, anonymous inner classes are usually used in window sinks.

3. Encapsulate the patch into a jar file
An important jar application is to improve the loading of the program pieces. In Java version 1.0, people tend to try to fill their code into a single piece of the program, so customers need only a single server to download the code for the program. But this not only makes the results messy, difficult to read (of course, maintenance also) program, but the class file has been unable to compress, so the download has never been faster.
The jar file packs all of our compressed class files into a single file, which is then downloaded by the browser. Now we don't need to create a bad design to minimize the classes we create, and the user will get faster download speed.
To think about the above example, this example looks like BUTTON2NEWB, a single class, but in fact it contains three internal classes, so there are four. Whenever we compile a program, I use this line of code to package it into a jar file:
Jar CF Button2newb.jar *.class
This assumes that there is only one class file in the current directory, one of which is from Button2newb.java (otherwise we will get a special package).
Now we can create an HTML page that uses the new file tag to specify the jar file, as follows:

<HEAD><TITLE>BUTTON2NEWB Example Applet
</title> 
 

Any other content related to the markup tag in the HTML file remains unchanged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.