12.3. Wie man an das ActionEvent eines Buttons kommt

❶ Implementieren Sie das Interface ActionListener.

❷ Registrieren Sie sich beim Button (teilen Sie ihm mit, dass Sie auf Events lauschen wollen).

❸ Definieren Sie die Event-Handling-Methode (implementieren Sie die Methode actionPerformed() aus dem Interface ActionListener).

import javax.swing.*;
import java.awt.event.*;

public class EinfacheGui1B implements ActionListener {
  JButton button;

  public static void main (String[] args) {
    EinfacheGui1B gui = new EinfacheGui1B();
    gui.los();
  }

  public void los() {
    JFrame frame = new JFrame();
    button = new JButton("klick mich");

    button.addActionListener(this); frame.getContentPane().add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); ...

Get Java™ von Kopf bis Fuß now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.