I'm trying to make a lambda expresion for an ActionListener, but it gives me an IllegalStart expression, what I'm trying to run so far is as follows:
JFrame frame = new JFrame(); JButton boton = new JButton("Lambda Button"); boton.addActionListener(event -> System.out.println("Hello World!")); frame.add(boton); frame.setVisible(true);
On the other hand, when I use this code instead:
JFrame frame = new JFrame(); JButton boton = new JButton("Lambda Button"); boton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello World!"); } } ); frame.add(boton); frame.setVisible(true);
It works great
I initially decided that the problem might be the version of java that I run, but I just update and keep doing the same when I do the java version, I give me the following:
java version java version "1.8.0_45" Java (TM) SE Runtime Environment (build 1.8.0_45-b14) Java client virtual machine HotSpot (TM) (build 25.45-b02, mixed mode)
So, as far as I know, he has a version compatible with the lambda expression, but not succeeding in getting them to work, any ideas or suggestions about what he could do?
EDIT: When I try to compile, I get the following:
Prueba.java:57: error: illegal start of expression boton.addActionListener(event -> System.out.println("Hello World !")); ^1 error
EDIT2: I am not using an IDE, im compiling from the command line
source share