Home>

First, event monitoring

Test code one:

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testtextfield {
  public static void main (string args []) {
    new myframetextfield ();
  }
}
class myframetextfield extends frame {
  myframetextfield () {
    textfield tf=new textfield ();
    add (tf);
    tf.addactionlistener (new monitor3 ());
    tf.setechochar ("*");
    /*
     * The setechochar () method is to set the characters displayed when the text box is entered,Set here to *,     * In this way, anything you enter is displayed as *,But you can still see what you type when you print it out
     * /
    setvisible (true);
    pack ();
  }
}
class monitor3 implements actionlistener {
  /*
   * All methods in the interface are public
   * So when copying void actionperformed (actionevent e) from the api document, add public in front of void
   * /
  public void actionperformed (actionevent e) {
    /* The relevant information of the event is encapsulated in the object e. The relevant information of the object e can be used to obtain the relevant information of the event * /
    textfield tf=(textfield) e.getsource ();
    /*
     * The getsource () method is to get the event source,Note:When you get this event source,     * Treat it as the parent of textfield
     * The definition of the getsource () method is:"public object getsource ()" The return value is an object object,     * So cast to an object of type textfield
     * If i want to access the event source object of another class in one class, you can use the getsource () method.
     * /
    system.out.println (tf.gettext ());//tf.gettext () is to get the contents of the text box
    tf.settext ("");//clear the contents of the text box
  }
}

Test code two:

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testactionevent2 {
  public static void main (string args []) {
    frame f=new frame ("testactionevent");
    button btn1=new button ("start");
    button btn2=new button ("stop");
    monitor2 m2=new monitor2 ();//Create a monitor object
    btn1.addactionlistener (m2);
    /* A listener listens to two buttons at the same time * /
    btn2.addactionlistener (m2);
    btn2.setactioncommand ("gameover");//Set the return information after executing the btn2 click command
    f.add (btn1, borderlayout.north);
    f.add (btn2, borderlayout.center);
    f.pack ();
    f.setvisible (true);
  }
}
class monitor2 implements actionlistener {
  public void actionperformed (actionevent e) {
    system.out.println ("a button has been pressed," + "the relative info is:\ n" + e.getactioncommand ());
    /* Use the returned listener object e to call the getactioncommand () method to get the return information after the two buttons execute the command
    Differentiate which button is currently operating according to the different returned information,btn1 is not set using the setactioncommand () method
    Then the information returned by btn1 is the text displayed on the button * /
  }
}

Second, the textfield event listener

Test code:

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testtextfield {
  public static void main (string args []) {
    new myframetextfield ();
  }
}
class myframetextfield extends frame {
  myframetextfield () {
    textfield tf=new textfield ();
    add (tf);
    tf.addactionlistener (new monitor3 ());
    tf.setechochar ("*");
    /*
     * The setechochar () method is to set the characters displayed when the text box is entered,Set here to *,     * In this way, anything you enter is displayed as *,But you can still see what you type when you print it out
     * /
    setvisible (true);
    pack ();
  }
}
class monitor3 implements actionlistener {
  /*
   * All methods in the interface are public
   * So when copying void actionperformed (actionevent e) from the api document, add public in front of void
   * /
  public void actionperformed (actionevent e) {
    /* The relevant information of the event is encapsulated in the object e, and the relevant information of the object e can be used to obtain the relevant information of the event * /
    textfield tf=(textfield) e.getsource ();
    /*
     * The getsource () method is to get the event source,Note:When you get this event source,     * Treat it as the parent of textfield
     * The definition of the getsource () method is:"public object getsource ()" The return value is an object object,     * So cast to an object of type textfield
     * If i want to access the event source object of another class in one class, you can use the getsource () method.
     * /
    system.out.println (tf.gettext ());//tf.gettext () is to get the contents of the text box
    tf.settext ("");//clear the contents of the text box
  }
}

Simple calculator using textfield class

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testmath {
  public static void main (string args []) {
    new tfframe ();
  }
}
/* Here is mainly to complete the layout of the calculator elements * /
class tfframe extends frame {
  tfframe () {
    /*
     * Create 3 text boxes,And specify its initial size to be 10 characters and 15 characters in size. Here is another construction method of the textfield class public textfield (int columns)
     * /
    textfield num1=new textfield (10);
    textfield num2=new textfield (10);
    textfield num3=new textfield (15);
    /* Create equal button * /
    button btnequal=new button ("=");
    btnequal.addactionlistener (new mymonitor (num1, num2, num3));
    /* Add a monitor to the equals button,Make a response event after clicking the button * /
    label lblplus=new label ("+");
    /* "+" Is a static text,So use the label class to create a static text object * /
    setlayout (new flowlayout ());
    /* Change the frame's default borderlayout layout to flowlayout layout * /
    add (num1);
    add (lblplus);
    add (num2);
    add (btnequal);
    add (num3);
    pack ();
    setvisible (true);
  }
}
class mymonitor implements actionlistener {
  textfield num1, num2, num3;
  /*
   * In order to make the monitoring of the button also work for the text box,   * So define three objects of type textfield num1, num2, num3, in the custom class mymonitor
   * And defines a constructor of the mymonitor class. This constructor takes three parameters of type textfield,   * Used to receive three textfield type parameters passed from the tfframe class
   * Then assign the three textfield parameters received to the three textfield parameters num1, num2, num3 declared in this class, and then process num1, num2, num3 in the actionperformed () method
   * /
  public mymonitor (textfield num1, textfield num2, textfield num3) {
    this.num1=num1;
    this.num2=num2;
    this.num3=num3;
  }
  public void actionperformed (actionevent e) {
    /* The relevant information of the event is encapsulated in the object e, and the relevant information of the object e can be used to obtain the relevant information of the event * /
    int n1=integer.parseint (num1.gettext ());/* The num1 object calls the gettext () method to get the text string it displays * /
    int n2=integer.parseint (num2.gettext ());/* The num2 object calls the gettext () method to get the text string it displays * /
    num3.settext ("" + (n1 + n2));/* num3 object calls settext () method to set its own display text * /
    num1.settext ("");
    /* Clear the contents of the num1 and num2 text boxes after the calculation ends * /
    num2.settext ("");
    //num3.settext (string.valueof ((n1 + n2)));
    /* When a string is connected to any type of data using "+", it must be a string.
     * Here an empty string is used to concatenate a number of type int,In this way, you can directly convert the int type number obtained by (n1 + n2) into a string.
     * This is a little trick to convert other basic data types into strings.
     * You can also use "string.valueof ((n1 + n2))" to convert the sum of (n1 + n2) into a string
     * /
  }
}

Classic usage in java:holding a reference to another class in one class

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testmath1 {
  public static void main (string args []) {
    new ttmyframe (). launchframe ();
    /* Call the lauchframe () method after creating the ttmyframe object to display the calculator window * /
  }
}
/* Make the calculator form interface * /
class ttmyframe extends frame {
  /* Encapsulate the code for designing the calculator form into a method * /
  textfield num1, num2, num3;
  public void launchframe () {
    num1=new textfield (10);
    num2=new textfield (15);
    num3=new textfield (15);
    label lblplus=new label ("+");
    button btnequal=new button ("=");
    btnequal.addactionlistener (new mymonitorbtnequal (this));
    setlayout (new flowlayout ());
    add (num1);
    add (lblplus);
    add (num2);
    add (btnequal);
    add (num3);
    pack ();
    setvisible (true);
  }
}
/*
 * Here by getting a reference to the ttmyframe class,Then use this reference to access the member variables in the ttmyframe class
 * This method is much better than the previous method to directly access the member variables in the ttmyframe class.
 * Because you don't need to know what member variables are in the ttmyframe class now, * Now to access the member variables in the ttmyframe class,Just use the reference of the ttmyframe class object to access it, * This ttmyframe object is like a big housekeeper, And I told the steward,I want to access those member variables in the ttmyframe class, * The butler's quote will help me find it,I no longer need to find it myself.
 * This usage of holding a reference to another class in one class is a very typical usage
 * Use the obtained reference to access all members of one class in another class
 * /
class mymonitorbtnequal implements actionlistener {
  ttmyframe ttmf=null;
  public mymonitorbtnequal (ttmyframe ttmf) {
    this.ttmf=ttmf;
  }
  public void actionperformed (actionevent e) {
    int n1=integer.parseint (ttmf.num1.gettext ());
    int n2=integer.parseint (ttmf.num2.gettext ());
    ttmf.num3.settext ("" + (n1 + n2));
    ttmf.num1.settext ("");
    ttmf.num2.settext ("");
  }
}

The results are as follows:

Third, the inner class

Examples of use of inner classes:

package cn.javastudy.summary;
import java.awt. *;
import java.awt.event. *;
public class testmath3 {
  public static void main (string args []) {
    new mymathframe (). launchframe ();
  }
}
class mymathframe extends frame {
  textfield num1, num2, num3;
  public void launchframe () {
    num1=new textfield (10);
    num2=new textfield (15);
    num3=new textfield (15);
    label lblplus=new label ("+");
    button btnequal=new button ("=");
    btnequal.addactionlistener (new mymonitor ());
    setlayout (new flowlayout ());
    add (num1);
    add (lblplus);
    add (num2);
    add (btnequal);
    add (num3);
    pack ();
    setvisible (true);
  }
  /*
   * This mymonitor class is an inner class,It defines the myframe class inside the myframe class. A wrapper class called mymonitor class.
   * /
  /*
   * Benefits of using inner classes:
   * The first great benefit is unobstructed access to all member variables and methods of the outer class (i.e. the wrapper class of the inner class)
   * As the three member variables num1, num2, num3 defined in the myframe class (external class),   * Can be accessed directly in mymonitor (inner class)
   * This is equivalent to the internal class object having a reference to the external class object by default when creating the external class object
   * /
  private class mymonitor implements actionlistener {
    public void actionperformed (actionevent e) {
      int n1=integer.parseint (num1.gettext ());
      int n2=integer.parseint (num2.gettext ());
      num3.settext ("" + (n1 + n2));
      num1.settext ("");
      num2.settext ("");
    }
  }
}

The great benefits of inner classes are:

Easy access to member variables and methods defined by external classes When a class does not need access by other classes, declare this class as an inner class.

Fourth, the graphics class

Test code:

package cn.javastudy.summary;
import java.awt. *;
public class testpaint {
  public static void main (string args []) {
    new mypaint (). launchframe ();
    /* The paint (graphics g) method is not explicitly called in the main () method
    But when the frame form is created, I can see that it is drawn on the frame form.
    Circles and rectangles,This is because the paint () method is a special method
    It is automatically implicitly called when the frame form is created
    When we minimize the frame window and open it again,Will call again
    paint () method repaints circles and rectangles on the frame window
The paint () method will be called automatically every time you need to redraw the frame window
  }
}
class mypaint extends frame {
  public void launchframe () {
    setbounds (200,200,640,480);
    setvisible (true);
  }
  public void paint (graphics g) {
    /* paint (graphics g) method has a graphics parameter g
    We can think of this g as a painter,The painter holds a paintbrush in his hand
    We set the colors and shapes of the brushes to draw the various images we want * /
    /* Set the color of the brush * /
    g.setcolor (color.red);
    g.filloval (100,100,100,100);/* Draw a solid ellipse * /
    g.setcolor (color.green);
    g.fillrect (150,200,200,200);/* Draw a solid rectangle * /
    /* The following two lines of code are written for good programming practice of writing programs
    The color of the brush is set in the front,It's time to restore the initial color of the brush
    It is equivalent to clearing the colors on the paintbrush after the painter has finished using it * /
    color c=g.getcolor ();
    g.setcolor (c);
  }
}

operation result:

Five, mouse event adapter

Test code:

package cn.galc.test;
import java.awt. *;
import java.awt.event. *;
import java.util. *;
public class mymouseadapter {
 public static void main (string args []) {
  new myframe ("drawing ...");
 }
}
class myframe extends frame {
 arraylist points=null;
 myframe (string s) {
  super (s);
  points=new arraylist ();
  setlayout (null);
  setbounds (300,300,400,300);
  this.setbackground (new color (204,204,255));
  setvisible (true);
  this.addmouselistener (new monitor ());
  }
  public void paint (graphics g) {
  iterator i=points.iterator ();
  while (i.hasnext ()) {
   point p=(point) i.next ();
   g.setcolor (color.blue);
   g.filloval (p.x, p.y, 10, 10);
  }
 }
 public void addpoint (point p) {
  points.add (p);
 }
}
class monitor extends mouseadapter {
 public void mousepressed (mouseevent e) {
  myframe f=(myframe) e.getsource ();
  f.addpoint (new point (e.getx (), e.gety ()));
  f.repaint ();
 }
}

Window event

Test code:

package cn.galc.test;
import java.awt. *;
import java.awt.event. *;
public class testwindowclose {
 public static void main (string args []) {
  new windowframe ("Close windowframe");
 }
}
class windowframe extends frame {
 public windowframe (string s) {
  super (s);
  setbounds (200,200,400,300);
  setlayout (null);
  setbackground (new color (204,204,255));
  setvisible (true);
  this.addwindowlistener (new windowmonitor ());
/* Monitor the actions of this form,Encapsulate all action information into an object and pass it to the listener class * /
  this.addwindowlistener (
  /* Define a class in a method,This class is called a local class,Also called anonymous inner class,  The code in {... code ...} is very similar to the body of a class.
It's just that this class has no name,So called anonymous class
  Here is to use this anonymous class as a windowadapter class,The essential meaning of this syntax is equivalent to this anonymous class
  Inherited from the windowadapter class, now an object of an anonymous class is created and used as a windowadapter
  When this anonymous class comes out (), no one knows * /
   new windowadapter () {
    public void windowclosing (windowevent e) {
     setvisible (false);
     system.exit (-1);
    }
   }
  );
 }
 /* Here the listener class is also defined as an inner class * /
 class windowmonitor extends windowadapter {
  /* windowadapter (window adapter) class implements the windowlistener listening interface
  Override all methods in the windowlistener interface
  If you use the custom windowmonitor class directly, go directly
  To implement the windowlistener interface, you must override the windowlistener interface
  All the methods inside,But now only one of these methods is needed
  Therefore, a subclass of windowlistener listening interface is implemented by inheritance.
  And override the method needed in this subclass
  This approach is much more concise and convenient than directly rewriting many unused methods to directly implement the windowlistener listening interface * /
/* Rewrite the required windowclosing (windowevent e) method * /
  public void windowclosing (windowevent e) {
    setvisible (false);/* Set the form to not show,You can close the form * /
    system.exit (0);/* Exit normally * /
   }
 }
}

Seven, keyboard response events-keyevent

Test code:

package cn.galc.test;
import java.awt. *;
import java.awt.event. *;
public class testkeyevent {
 public static void main (string args []) {
  new keyframe ("Keyboard response event");
 }
}
class keyframe extends frame {
 public keyframe (string s) {
   super (s);
   setbounds (200,200,400,300);
   setlayout (null);
   setvisible (true);
   addkeylistener (new keymonitor ());
  }
 /* Define the custom keyboard listening class as an inner class
 This listener class inherits from the keyboard adapter keyadapter class
 Inheriting from the keyadapter class is also for simplicity and convenience
 Just rewrite the methods you need,This approach is better than
 It is simple and convenient to directly implement the keylistener interface,in case
 To directly implement the keylistener interface, you must put the keylistener
 Rewrite all methods in the interface,But really used
 There is only one way,Rewrite other methods like this but not needed
 Will inevitably do useless work * /
 class keymonitor extends keyadapter {
  public void keypressed (keyevent e) {
    int keycode=e.getkeycode ();
   /* Use the getkeycode () method to get the virtual code of the key * /
   /* If the virtual code of the obtained key is equal to the virtual code of the up key
   It means that the currently pressed key is up
   keyevent.vk_up represents the virtual code to get the up key
   Each key in the keyboard corresponds to a virtual code
   These virtual codes are defined as static constants in the keyevent class.
   So you can use the form of "class name. Static constant name" to get these static constants * /
    if (keycode == keyevent.vk_up) {
      system.out.println ("You pressed the up key");
     }
   }
  }
}
/* The processing event of the keyboard is this:each key corresponds to a virtual code,When a key is pressed,The system will find the virtual code corresponding to this key,To determine which key is currently being pressed
* /
  • Previous Installation and uninstallation of Android programming software
  • Next Android programming example of live wallpaper
  • Trends