Supplies for Java action

Source: Internet
Author: User

As noted earlier, action () is not the only way to automatically call Handleevent () when we categorize everything. There are three other called set of methods, and if we want to catch certain types of events (keyboard, mouse, and focus events), we have to overload the prescribed method. These methods are defined in the underlying class components, so they are useful in almost all of the components that we might have placed in the form. However, we also note that this approach is not supported in Java version 1.1, and although we may notice that inheritance code takes advantage of this approach, we will use the Java 1.1 version instead (described later in this chapter).

When the component method calls

Action (event evt, Object what) when a typical event occurs for a component (for example, when a button is pressed or the Drop-down list item is selected)
KeyDown (Event evt, int key) is invoked when the key is pressed and the component has focus. The second argument is a pressed key and is redundant from the Evt.key.
KeyUp (Event evt, int key) Called when the key is released and the component has focus
LostFocus (Event evt, Object what) is called when the focus is moved away from the target. Usually, the what is duplicated from the Evt.arg.
GotFocus (Event evt, object What) is called when the focus is moved to the target
MouseDown (Event evt, int x,int y) a mouse is pressed on top of the component and called at the X,y coordinates
MouseUp (Event evt, int x, int y) called when a mouse rises over a component
MouseMove (Event evt, int x, int y) called when the mouse moves on a component
Mousedrag (event evt, int x, int y) the mouse drags after a MouseDown event occurs. All drag events are reported to the component where the MouseDown event occurred, until a MouseUp is encountered
MouseEnter (Event evt, int x, int y) the mouse was not previously on the component, but is currently
Mouseexit (Event evt, int x, int y) the mouse was once located above the component, but is not currently

When we deal with special situations--a mouse event, for example, it happens to be the coordinates of the mouse event we want, we'll see that each program receives an event along with some of the information we need. Interestingly, when the Handleevent () of the component calls these methods (typical cases), additional arguments are always superfluous because they are contained in the event object. In fact, if we look at the source code of Component.handleevent (), we can see that it obviously will increase the arguments to extract the event object (this may be taken into account in some languages inefficient coding, but remember that Java focus is safe, don't worry.) The experiment shows us that these events are actually called and as an interesting attempt is worth creating an overload of each method of the program, (Action () overload in other parts of this chapter) when the event occurs when the relevant data is displayed.
This example also shows us how to make our own button object, because it is used as a target for all event interests. I may first (and must) assume the creation of a new button that we inherit from the button. But it's not going to work. Instead, we inherit from the canvas component (a very common component) and do not use the paint () method to draw a button on it. As we can see, since some of the code has been mixed into the drawing buttons, the button doesn't work at all, which is really bad. (If you don't believe me, trying to swap the button for the canvas component in the example, remember to call the base class builder called Super.) We will see that the button will not be drawn and the event will not be processed. )
The MyButton class is explicitly stated: It runs only with an automatic event (autoevent) "Parent window" (the parent window is not a base class, it is a window that creates and exists a button.) )。 With this knowledge, MyButton may enter the parent window and process its text fields, and it is necessary to write state information to the fields of the parent window. Of course, this is a very limited solution, MyButton can only be used when connecting autoevent. This code is sometimes referred to as "highly binding." However, manufacturing mybutton requires much more than an effort to guarantee an example (and possibly some of the pieces we will write). Again, note that the following code uses APIs that are not supported in Java version 1.1.

: Autoevent.java//Alternatives to Action () import java.awt.*;
Import java.applet.*;

Import java.util.*;
  Class MyButton extends Canvas {autoevent parent;
  Color color;
  String label;
    MyButton (autoevent parent, color color, String label) {this.label = label;
    This.parent = parent;
  This.color = color;
    public void Paint (Graphics g) {g.setcolor (color);
    int rnd = 30;
    G.fillroundrect (0, 0, size (). width, size (). Height, Rnd, RND);
    G.setcolor (Color.Black);
    G.drawroundrect (0, 0, size (). width, size (). Height, Rnd, RND);
    FontMetrics fm = G.getfontmetrics ();
    int width = fm.stringwidth (label);
    int height = fm.getheight ();
    int ascent = Fm.getascent ();
    int leading = fm.getleading ();
    int horizmargin = (size (). width-width)/2;
    int vermargin = (size (). height-height)/2;
    G.setcolor (Color.White); g.DrawString (Label, Horizmargin, Vermargin + ascent+ leading);
    public boolean KeyDown (Event evt, int key) {TextField T = (TextField) parent.h.get ("KeyDown");
    T.settext (Evt.tostring ());
  return true;
    public boolean keyUp (Event evt, int key) {TextField T = (TextField) parent.h.get ("KeyUp");
    T.settext (Evt.tostring ());
  return true;
    public boolean LostFocus (Event evt, Object W) {TextField t = (TextField) parent.h.get ("LostFocus");
    T.settext (Evt.tostring ());
  return true;
    public boolean GotFocus (Event evt, Object W) {TextField t = (TextField) parent.h.get ("GotFocus");
    T.settext (Evt.tostring ());
  return true;
    public boolean MouseDown (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("MouseDown");
    T.settext (Evt.tostring ());
  return true;
    public boolean Mousedrag (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("Mousedrag");
    T.settext (Evt.tostring ());
  return true;
}  public boolean mouseEnter (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("MouseEnter");
    T.settext (Evt.tostring ());
  return true;
    public boolean mouseexit (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("Mouseexit");
    T.settext (Evt.tostring ());
  return true;
    public boolean mouseMove (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("MouseMove");
    T.settext (Evt.tostring ());
  return true;
    public boolean mouseUp (Event evt,int x,int y) {TextField t = (TextField) parent.h.get ("MouseUp");
    T.settext (Evt.tostring ());
  return true;
  } public class Autoevent extends Applet {Hashtable h = new Hashtable (); String[] event = {"KeyDown", "KeyUp", "LostFocus", "GotFocus", "MouseDown", "MouseUp", "MouseMove", "moused
  Rag "," MouseEnter "," Mouseexit "}; MyButton B1 = new MyButton (this, Color.Blue, "Test1"), b2 = new MyButton(This, color.red, "test2");
    public void init () {setlayout (new GridLayout (event.length+1,2));
      for (int i = 0; i < event.length i++) {TextField t = new TextField ();
      T.seteditable (FALSE); 
      Add (New Label (Event[i], label.center));
      Add (t);
    H.put (Event[i], T);
    Add (B1);
  Add (B2); }
} ///:~

We can see that the builder uses a method with the same name as a variable, so the argument is assigned and uses this to differentiate between:
This.label = label;
The paint () method starts with a simple start: it fills a "rounded rectangle" with the color of the button, and then draws a black line around it. Note that the use of size () determines the width and length of the component (in pixels, of course). After that, paint () looks very complicated, because there are a lot of predictions to figure out how to use the label of the "Font metrics" set button into the button. We can get a pretty good idea of continuing to focus on the method invocation, which picks out the fairly mundane code in the program, and when we want to focus a tag into some components, we can just cut and paste it.
You are not aware that the Autoevent class can correctly understand the operation of KeyDown (), KeyUp (), and other methods. This includes a hashtable to control strings to describe events and TextField types for event handling. Of course, these can be created statically rather than put into Hashtable but I think you'll agree that it's easier to use and change. In particular, if we need to add or remove a new event type in autoevent, we simply add or remove a string in the event queue-all the work is done automatically.
We find the position of the string in KeyDown (), KeyUp () and other methods back to MyButton. Either of these methods attempts to return to the parent window with the parent handle. The parent class is a autoevent that contains the Hashtable H and get () methods, and when a particular string is in place, a handle is generated for a TextField object that we know (so it is selected). The event object then modifies the string representation displayed in TextField. This example can be quite interesting to run from when we can really notice that the example we are running events in our program.

Related Article

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.