ActionEvent Scrollbar AdjustmentEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent ScrollPane ContainerEvent,...

Linki


» Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
»
In another mechanism, the action yyclearin clears the current symbol after an error which follows the error entity...
»
Simple Frame Actions lub Simple Buttons w menu Control nie spowoduje wykonanianowych akcji w skryptach...
»
Action Check the statement for a typing mistake in the filename and check for the existence of all files...
»
Action Rewrite the trigger or function so it does not read the table...
»
Action This thread is not required for the operation of the database...
»
Teoria strun ma dziwną historię...
»
łem się po szczeniaka, a ona skoczyła na mnie...
»
To już 125 lat ! Wreszcie nadszedł rok 2000 a z nim 125 lat białostockiej Straży Pożarnej...
»
Na tej wieży albo raczej sali, królowie uciechy swoje miewali, kolacye jadali, tańce i różne odprawowali rekreacye, bo jest w ślicznym bardzo prospekcie 5: może z...
»
I znowu upłynął czas, aż wreszcie Robin raz jeszcze zapytał młodego Dawida, co słychać za murem, a Dawid rzekł:— Słyszę kukułkę i widzę, jak wiatr...

Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.

You simply:
1. Take the name of the event class and remove the word “Event.” Add the word “Listener”
to what remains. This is the listener interface you need to implement in your inner class.
2. Implement the interface above and write out the methods for the events you want to capture. For example, you might be looking for mouse movements, so you write code for the mouseMoved( ) method of the MouseMotionListener interface. (You must implement the other methods, of course, but there’s a shortcut for that which you’ll see soon.)
3. Create an object of the listener class in step 2. Register it with your component with the method produced by prefixing “add” to your listener name. For example,
addMouseMotionListener( ).
To finish what you need to know, here are the listener interfaces:
514
Thinking in Java
www.BruceEckel.com
Listener interface
Methods in interface
w/ adapter
ActionListener
actionPerformed(ActionEvent)
AdjustmentListener
adjustmentValueChanged(
AdjustmentEvent)
ComponentListener
componentHidden(ComponentEvent)
ComponentAdapter
componentShown(ComponentEvent)
componentMoved(ComponentEvent)
componentResized(ComponentEvent)
ContainerListener
componentAdded(ContainerEvent)
ContainerAdapter
componentRemoved(ContainerEvent)
FocusListener
focusGained(FocusEvent)
FocusAdapter
focusLost(FocusEvent)
KeyListener
keyPressed(KeyEvent)
KeyAdapter
keyReleased(KeyEvent)
keyTyped(KeyEvent)
MouseListener
mouseClicked(MouseEvent)
MouseAdapter
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
MouseMotionListener
mouseDragged(MouseEvent)
MouseMotionAdapter
mouseMoved(MouseEvent)
WindowListener
windowOpened(WindowEvent)
WindowAdapter
windowClosing(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
ItemListener
itemStateChanged(ItemEvent)
TextListener
textValueChanged(TextEvent)
Using listener adapters for simplicity
In the table above, you can see that some listener interfaces have only one method. These are trivial to implement since you’ll implement them only when you want to write that particular method. However, the listener interfaces that have multiple methods could be less pleasant to use. For example, something you must always do when creating an application is provide a WindowListener to the Frame so that when you get the windowClosing( ) event you can call System.exit(0) to exit the application. But since WindowListener is an interface, you must implement all of the other methods even if they don’t do anything. This can be annoying.
To solve the problem, each of the listener interfaces that have more than one method are provided with adapters, the names of which you can see in the table above. Each adapter provides default methods for each of the interface methods. (Alas, WindowAdapter does not have a default windowClosing( ) that calls System.exit(0).) Then all you need to do is inherit from the adapter and override only the methods you need to change. For example, the typical WindowListener you’ll use looks like this:
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Chapter 13: Creating Windows & Applets
515
System.exit(0);
}
}
The whole point of the adapters is to make the creation of listener classes easy.
There is a downside to adapters, however, in the form of a pitfall. Suppose you write a WindowAdapter like the one above:
class MyWindowListener extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
}
This doesn’t work, but it will drive you crazy trying to figure out why, since everything will compile and run fine – except that closing the window won’t exit the program. Can you see the problem? It’s in the name of the method: WindowClosing( ) instead of windowClosing( ). A simple slip in capitalization results in the addition of a completely new method. However, this is not the method that’s called when the window is closing, so you don’t get the desired results.
Making windows and applets
with the Java 1.1 AWT

Powered by MyScript