Java ActionListener implement

Description

Java ActionListener implement

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    JButton closeButton1 = new JButton("Close");
    closeButton1.addActionListener(new ButtonHandler());
    getContentPane().add(closeButton1);/*  ww  w .ja  v  a2  s . c  o m*/
  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}

class ButtonHandler implements ActionListener {
  // handle button event
  @Override
  public void actionPerformed(ActionEvent event) {
    System.exit(0);
  }
}



PreviousNext

Related