JavaGUI 05 event monitoring & & solving Chinese garbled code

3.1 event monitoring

3.1.1 understanding and learning

We monitor what we need to do when something happens. This is "event monitoring"

  • Close form (event listening)

We've written about event monitoring for closing forms before. What's its format?

frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

This is done by calling the addWindowListener method through the frame object we created. This method has a parameter to receive a listener object! This object has many methods, which are also called "events".


The above created object and the event to be monitored (write method) are in the form of anonymous inner class. (in this way, we don't recommend writing, but we can't know and won't write like this. We recommend developers to write a subclass implements listening interface, and then use this subclass to create a listener object!)

  • Each component and container has events

Every component and container has events! Even the simplest buttons have click events. The way they add events is similar to the way they close the form!

For example, the format of the button click event is

button. Addactionlistener (listener for the button);

The listener of each component and container may also be different. The type of this listener depends on the name of the add event method of the component and container. They are usually very similar.

package com.muquanyu.leeson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent {
    public static void main(String[] args) {
        //For example, press a button to trigger some events

        Frame frame = new Frame();
        Button button = new Button("Please click ~");
        MyActionListener m = new MyActionListener();
        button.addActionListener(m);
        frame.add(button);

        frame.setVisible(true);
    }
}

class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("The button has been clicked!");
    }
}


This event is triggered. But there is a problem of Chinese garbled code!!!

3.1.2 solution of Chinese garbled code in AWT form

  1. Setting --> Build... --> Comiler --> Shared build process VM options
  2. Search for File Encodings and set them all to UTF-8

But in the end, I still didn't find that the coding was normal! It proves that AWT has always been unfriendly to Chinese.

3.1.3 the two buttons are the same as monitoring

Monitor the Start and Stop buttons, but only one event can be monitored!

package com.muquanyu.leeson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StartAndEnd {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button start = new Button("Start");
        Button stop = new Button("Stop");
        start.setActionCommand("Start");
        stop.setActionCommand("Stop");

        frame.setLayout(new FlowLayout());
        frame.add(start);
        frame.add(stop);

        StartAndStop sas = new StartAndStop();

        start.addActionListener(sas);
        stop.addActionListener(sas);

        frame.setVisible(true);
    }
}
class StartAndStop implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand() == "Stop")
        {
            System.out.println("Stop button clicked!");
        }else{
            System.out.println("Start button clicked!");
        }
    }
}

setActionCommand() returns a unique command code after the event is triggered. (equivalent to token Key) to tell us what the component or container that currently triggers the event is!!

Then we can make a judgment according to the returned Command. Although it is the same event, each time it is triggered, a different Command will be returned, which means that different buttons are triggering an event! This is called simultaneous monitoring (this method can improve efficiency and does not waste memory).

Keywords: Java GUI

Added by yonta on Wed, 19 Jan 2022 10:53:15 +0200