Java JButton handle click event via lambda expression

Description

Java JButton handle click event via lambda expression

import java.awt.FlowLayout;

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

public class Main extends JFrame {

  public Main() {
    super("demo from demo2s.com");
    JButton closeButton = new JButton("Close");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLayout(new FlowLayout());

    getContentPane().add(closeButton);/*from   w w w .  j a  v a2  s .  c om*/

    closeButton.addActionListener(e -> System.exit(0));
  }

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



PreviousNext

Related