Button-Beispiel 4 - Applet als Wirt und ActionListener eines Buttons

Das Applet implementiert das Interface ActionListener (durch Integration der Methode
actionPerformed). Damit kann das Applet sowohl den Button auf der Oberfläche tragen,
als auch die vom Button ausgesandten Events auffangen und verarbeiten.


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

public class EinButtonBeispiel_4 extends Applet implements ActionListener {

	protected Button bb;
	
	protected String buttonText1 = "Drück mich!";
	
	protected String text1 = "Ola Ola!!!";
	protected String text2 = "Wow!!!";
		
	// init() wird automatisch beim Laden des Applets
	// aufgerufen
	public void init() {
		
		bb = new Button( "Drück mich!" );		
		
		bb.addActionListener( this );
		
		add(bb);
	}

	public void changeButtonText() {
		String tempText = bb.getLabel();
		
		if( tempText.equals( text1 ) ) {
			bb.setLabel( text2 );	
		}
		else {
			bb.setLabel( text1 );
		}	
	}
	
	public void actionPerformed( ActionEvent e ) {
			this.changeButtonText();	
		}			
}