public class Main { public static void main(String[] args) { /* lambda expression = Lambda Expressions were added in Java 8. * is a short block of code which takes in parameters and returns a value. * Lambda expressions are similar to methods, but they do not need a name * and they can be implemented right in the body of a method. * also known as an anonymous method * a shorter way to write anonymous classes with only one method * * need to use a functional interface or use a pre-defined functional interface * they contain only one abstract method * ex. ActionListener * * can be used in any place where a functional interface is required * How to use a lambda expression: * (arguments) -> {statement/s} */
new MyFrame(); } } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; //public class MyFrame extends JFrame implements ActionListener{ public class MyFrame extends JFrame{
public MyFrame() { JButton btnButton = new JButton(); btnButton.setBounds(100, 100, 250, 150); btnButton.setText("Click me");
btnButton.addActionListener(e->System.out.println("I am from Button")); // btnButton.addActionListener(this); // btnButton.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // // TODO Auto-generated method stub // // System.out.println("I am from Button"); // } // });
this.setSize(500,500); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.add(btnButton); } // @Override // public void actionPerformed(ActionEvent e) { // // TODO Auto-generated method stub // System.out.println("I am from Button"); // } }
ty
Mashallah 😊
public class Main {
public static void main(String[] args) {
/* lambda expression = Lambda Expressions were added in Java 8.
* is a short block of code which takes in parameters and returns a value.
* Lambda expressions are similar to methods, but they do not need a name
* and they can be implemented right in the body of a method.
* also known as an anonymous method
* a shorter way to write anonymous classes with only one method
*
* need to use a functional interface or use a pre-defined functional interface
* they contain only one abstract method
* ex. ActionListener
*
* can be used in any place where a functional interface is required
* How to use a lambda expression:
* (arguments) -> {statement/s}
*/
new MyFrame();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//public class MyFrame extends JFrame implements ActionListener{
public class MyFrame extends JFrame{
public MyFrame() {
JButton btnButton = new JButton();
btnButton.setBounds(100, 100, 250, 150);
btnButton.setText("Click me");
btnButton.addActionListener(e->System.out.println("I am from Button"));
// btnButton.addActionListener(this);
// btnButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
//
// System.out.println("I am from Button");
// }
// });
this.setSize(500,500);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(btnButton);
}
// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// System.out.println("I am from Button");
// }
}