0% found this document useful (0 votes)
165 views7 pages

Action Listener

The document discusses different ways to implement an ActionListener in Java to handle button click events for a temperature conversion applet. It shows code examples of implementing ActionListener as an anonymous class, inner class, local class, and by having the applet class directly implement the ActionListener interface. The key points covered are how to pass the applet reference to the ActionListener implementation and invoke methods on the applet to perform the temperature calculation.

Uploaded by

dead_xxx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views7 pages

Action Listener

The document discusses different ways to implement an ActionListener in Java to handle button click events for a temperature conversion applet. It shows code examples of implementing ActionListener as an anonymous class, inner class, local class, and by having the applet class directly implement the ActionListener interface. The key points covered are how to pass the applet reference to the ActionListener implementation and invoke methods on the applet to perform the temperature calculation.

Uploaded by

dead_xxx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

import import import import

java.awt.*; java.applet.Applet; java.awt.event.ActionListener; java.awt.event.ActionEvent;

public class Temperatur extends Applet { Label inputPrompt, outputPrompt1, outputPrompt2; TextField input, output; double fahrenheit, celsius public void init() { // define Label and Textfields input,output input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calculateTemperature(); } }); // action will be on input field // add Labels and TextFields } public void calculateTemperature() { // fahrenheit = input number from TextField input; celsius = 5.0 / 9 * (fahrenheit - 32); // show celsius in TextField output } }
1

ActionListener als anonyme Klasse


input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calculateTemperature(); } }); // action will be on input field interface ActionListener { public void actionPerformed(ActionEvent e); }

Default Konstruktor, konstuiert Object, das ber Methode actionPerformed() verfgt

muss implementiert werden

{ public void actionPerformed(ActionEvent e) { calculateTemperature(); } }

anonyme Klasse
2

ActionListener als innere Klasse


public class Temperatur extends Applet { ... public void init() { // define Label and Textfields input, output input.addActionListener(new MyActionListener()); // add Labels and TextFields } public void calculateTemperature() { ... }

Default Konstruktor

public class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { calculateTemperature(); } } }

MyActionListener hat Klassenscope


3

ActionListener als lokale Klasse


public class Temperatur extends Applet { ... public void init() { // define Label and Textfields input, output class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { calculateTemperature(); } } input.addActionListener(new MyActionListener()); // add Labels and TextFields } public void calculateTemperature() { ... }

MyActionListener hat Blockscope


4

ActionListener als eigene Klasse (1)


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

public class Temperatur extends Applet { ... double fahrenheit, celsius public void init() { // define Label and Textfields input, output input.addActionListener(new MyActionListener()); input.addActionListener(new MyActionListener(this)); // add Labels and TextFields } public void calculateTemperature() { ... }

mssen Info aus Applet an Objekt geben, das actionPerformed() implementiert


5

ActionListener als eigene Klasse (2)


import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class MyActionListener implements ActionListener { private Temperatur tempApplet; public MyActionListener(Temperatur tempApplet) { this.tempApplet = tempApplet; } public void actionPerformed(ActionEvent e) { // perform the action calculateTemperature(); }

Referenz auf Applet als private Daten Konstruktor der Referenz erhlt Zugriff auf TextFields des Applet

public void calculateTemperature() { double fahrenheit = Double.parseDouble(tempApplet.input.getText()); // umrechnen tempApplet.output.setText(Double.toString(celsius)); } }

Applet Temperatur implementiert ActionListener


public class Temperatur extends Applet implements ActionListener { public void init() { // define Label and Textfields input, output input.addActionListener(this)); // add Labels and TextFields }

Angabe Objekt das ActionListener implementiert, kein Konstruktoraufruf

public void actionPerformed(ActionEvent e) { // perform the action calculateTemperature(); } public void calculateTemperature() { // fahrenheit = input number from TextField input; celsius = 5.0 / 9 * (fahrenheit - 32); // show celsius in TextField output } }
7

You might also like