6.2 Interface Instances
6.2.1 Interface and callback
There is a timer class in the Java.swing package that can be used to issue a notification at a given time interval, and if there is a clock in the program, you can request a notification every second to update the clock's dial.
When constructing a timer, it is necessary to set a time interval and tell the timer what to do when the time interval is reached (Java passes the object of a class to the timer, and then the timer invokes the object's method.) The-----timer needs to know which method is called and the class that the passed object belongs to implements the ActionListener interface of the Java.awt.event package.
1 Public Interface ActionListener 2 {3 void actionperformed (ActionEvent event); 4 }
When the specified time interval is reached, the timer calls the Actionperformed method.
Next, construct an object of the class and pass him to the timer constructor.
1 ActionListener listener =new timeprinter (); 2 Timer t =new timer (10000,listener);
The following program gives the timer and listener operation behavior, after the timer starts, the program will pop up a message dialog box, and wait for the user to click the OK button to terminate the operation of the program. Timing time is 5 seconds;
1 PackageCc.openhome;2 ImportJava.awt.Toolkit;3 Importjava.awt.event.ActionEvent;4 ImportJava.awt.event.ActionListener;5 Importjava.util.Date;6 ImportJavax.swing.JOptionPane;7 ImportJavax.swing.Timer;8 Public classTimertest {9 Public Static voidMain (string[] args) {TenActionListener listener =NewTimeprinter (); OneTimer t=NewTimer (5000, listener); A T.start (); -Joptionpane.showmessagedialog (NULL, "Quit program?"); -System.exit (0); the } - } - classTimeprinterImplementsActionListener - { + Public voidactionperformed (ActionEvent evet) - { +System.out.println ("At the tone, the time is" +NewDate ()); A Toolkit.getdefaulttoolkit (). Beep (); at } -}
At the tone, the time is Sun Mar 10:28:51 CST 201810:28:56 CST 201810:29:01 CST 2018 18 10:29:06 CST 201810:29:11 CST 201826 sec)
Run:
Api Javax.swing.JOptionPane 1.2
static void Showmessagedialog (ComPonent parent,object message)
Displays a dialog box containing a message and an OK button, which will be in the center of its parent component.
API javax.swing. Timer 1.2
Timer (int interval, ActionListener Listener)
Constructs a timer that advertises listener once every interval millisecond.
void Start ()
Start the timer. Once the boot is successful, the timer invokes the listener's actionperformed.
API void Stop ()
Stop the timer, and once the stop is successful, the timer will not be actionperformed to call the listener.
Java. Awt. Toolkit 1.0
Static Toolkit Getdefaulttoolkit ()
Get the default toolbox. The toolbox contains information about the GUI environment.
void Beep ()
Uttered a ringing bell.
6.2.2 Comparator interface
Suppose we want to sort the strings in increments of length rather than in dictionary order.
To handle this, the Arrays.sort method has a second version, an array and a comparator (comparator) as the parameter, and the comparer implements an instance of the comparator interface class.
Public Interface Comparator<t>{ int Compara (T first,second);}
To compare strings by length, you can define a class that implements comparator<string> as follows
class Implements Comparator<string>{ publicint Compare (String first,string second) { return first.length ()-second.length (); }}
When you complete the comparison, you need to create an instance:
Comparator<string> Comp =new lengthcomparator ();
if (Comp.compre (words[i],words[j]>0) ...
To sort an array, you need to pass in a Lengthcomparator object for the Arrays.sort method:
String[] Friengds ={"Peter", "Paul", "Mary"};
Arrays.sort (Friends,new lengthcomparator ());
Java Core Technology Volume One note 6.2 Interface lambda expression inner class