Java Capture Events

Source: Internet
Author: User

You can note that if you compile and run the above program, nothing happens when you press the button. You must go inside the program and write code that determines what is going to happen. For event-driven programming, the basic goal is to use code to catch events that occur and respond to those events by code. In fact, much of the GUI's content revolves around this event-driven program.
After learning from this book, you should have some foundation for object-oriented programming, where you might think of some object-oriented approaches to controlling events. For example, you might have to inherit each button and overload some "button down" methods (although this is very cumbersome and limited). You may also think that there are some master "event" classes that contain a method for each event that you want to respond to.
The typical way for event control before an object is a switch statement. Each event corresponds to a unique integer number, and in the Master event control method, a switch is written specifically for that value.
The AWT of Java 1.0 does not employ any object-oriented means. In addition, it does not use a switch statement and does not intend to rely on the numbers assigned to the event. Instead, we must create a nested series of IF statements. What we need to try to do with the IF statement is to detect objects that are "targets" for the event. In other words, that's all we care about-if a button is the target of an event, it's definitely a mouse click, and it's going to go on that assumption. However, other information may also be included in the event. For example, if you want to investigate the pixel position of a mouse click to draw a line that leads to that position, the event object contains information about that location (note also that the Java 1.0 component can only produce a limited variety of events, while Java 1.1 and swing/ The JFC component can produce a complete sequence of events.
The invocation of the action () method is present in the conditional statement in series in the AWT method of Java version 1.0. Although the entire Java 1.0 version of the event model is not compatible with Java version 1.1, it is more widely used in systems that do not support the Java1.1 version of the machine and run simple pieces of the program, and it can be very comfortable to use it, including the action () program method used below.
The action () has two arguments: the first is the type of the event, including all information about the event that triggered the call to action (). such as mouse clicks, normal press or release, special keystrokes pressed or released, mouse move or drag, event components get or lose focus, and so on. The second argument is usually the event target that we ignore. The second argument is encapsulated in the event target, so it is as verbose as an argument.
The situation is very limited when you call action (): When you place a control in a form, some types of controls (buttons, check boxes, drop-down lists, menus) have a "standard action" that initiates a call to action () with the appropriate event object. For example, for a button, once the button is pressed and no more presses, the action () method is invoked. This behavior is usually exactly what we want, because that's exactly what we look like to a button. But as we'll discuss later in this chapter, there are many other types of events that can be handled by the Handleevent () method.
The preceding routine can be extended to allow you to click on the control button as follows:

//: Button2.java//Capturing button presses import java.awt.*; import java.applet.*;
  public class Button2 extends Applet {button B1 = New button ("button 1"), B2 = New button ("button 2");
    public void Init () {Add (B1);
  Add (B2); Public boolean action (Event evt, Object Arg) {if (Evt.target.equals (B1)) getAppletContext (). Showstatus ("Butt
    On 1 ");
    else if (evt.target.equals (B2)) getAppletContext (). Showstatus ("button 2");
    Let's base class handle It:else return Super.action (EVT, ARG); return true; We ' ve handled it here}}///:~ 

To understand what the target is, you need to ask the event object what its target (target) member is, and then use the Equals () method to check whether it matches the target object handle that you are interested in. After you have written a handle on all objects of interest, you must call the Super.action (EVT, Arg) method in the Else statement at the end. As we have said in the 7th chapter (about the chapter on polymorphism), we are invoking a method that we overload, not its underlying class version. However, the underlying class version also provides the appropriate control code for all cases that we are not interested in. They are not invoked unless explicitly done. The return value indicates whether we have handled it, so if it does match an event, it should return true, otherwise it returns something returned by the underlying class event ().
For this example, the simplest action is to print out exactly what button is being pressed. Some systems allow you to pop up a small message window, but the Java patch prevents pop-up windows. However, we can use the getAppletContext () of the Applet method to access the browser, and then use Showstatus () to display a message (note ③) in the status bar at the bottom of the browser window. You can also print a complete description of the event in the same way, by calling Getappletconext (). Showstatus (evt + ""). An empty string forces the compiler to convert evt into a string. These reports are particularly useful for testing and debugging because browsers may overwrite our messages. The

③:showstatus () also belongs to a method of the applet, so you can call it directly without invoking getAppletContext ().

Although it may seem strange, we do have the ability to match an event with the text on the button through the second argument in the event (). With this approach, the example above becomes:
 

: Button3.java
//Matching events on button text
import java.awt.*;
Import java.applet.*;

public class Button3 extends Applet {
  button 
    B1 = New button ("button 1"), 
    b2 = New button ("button 2");
  public void init () {
    Add (B1);
    Add (b2);
  }
  Public boolean action (Event evt, Object Arg) {
    if (arg.equals ("button 1"))
      getAppletContext (). Showstatus (" Button 1 ");
    else if (arg.equals ("button 2"))
      getAppletContext (). Showstatus ("button 2");
    Let's base class handle it:
    else return 
      super.action (EVT, arg);
    return true; We ' ve handled it here
  }
}///:~

It's hard to know exactly what the Equals () method is doing here. One big problem with this approach is that Java programmers who start using this new technology need to spend at least a frustrating time comparing text on buttons to find that they either capitalize or write incorrectly (I have this experience). Similarly, if we change the text on the button, the program code will no longer work (but we will not get any compile-time and run-time information). So if possible, we have to avoid using this method.

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.