Lambda表达式在Java 8中不起作用? [英] Lambda expressions don't work in Java 8?
问题描述
我有一台运行Windows XP SP3 32位的虚拟机。在这台机器上
我从这里安装了Java SE JDK 8 build b44开发人员预览版。
I have a virtual machine running Windows XP SP3 32-bit. On this machine I installed the Java SE JDK 8 build b44 Developer Preview from here.
我还安装了JavaFX 2.1 SDK。
I also installed the JavaFX 2.1 SDK.
工作正常:
java -version
> java version "1.8.0-ea"
> Java(TM) SE Runtime Environment (build 1.8.0-ea-b44)
> Java HotSpot(TM) Client VM (build 24.0-b14, mixed mode, sharing)
我试过运行以下程序(取自这里):
I tried running the following program (taken from here):
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class LambdasWithJavaFx extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override public void start(Stage stage) throws Exception
{
BorderPane root = new BorderPane();
ToggleButton button = new ToggleButton("Click");
final StringProperty btnText = button.textProperty();
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent actionEvent)
{
ToggleButton source = (ToggleButton) actionEvent.getSource();
if (source.isSelected())
{
btnText.set("Clicked!");
}
else
{
btnText.set("Click!");
}
}
});
root.setCenter(button);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(200);
stage.setHeight(200);
stage.show();
}
}
程序编译并按预期运行。
The program compiled and ran as expected.
我按照该文章中的说明更换了按钮事件处理代码:
I followed the instructions in that article and replaced the button event-handling code with this:
button.setOnAction((ActionEvent event)->
{
ToggleButton source = (ToggleButton) event.getSource();
if (source.isSelected())
{
btnText.set("Clicked!");
}
else
{
btnText.set("Click!");
}
});
编译时,我收到以下错误(在行 button.setOnAction ((ActionEvent事件) - >
):
When compiling, I get the following error (on the line button.setOnAction((ActionEvent event)->
):
> lambda expressions are not supported in -source 1.8
> (use -source 8 or higher to enable lambda expressions)
I添加了参数 -source 8
,没有任何改变。
I added the argument -source 8
, nothing changed.
我只想检查Java中的lambda表达式功能8.为什么不起作用?
All I wanted was to check the lambda expressions functionality in Java 8. Why doesn't it work ?
推荐答案
您需要下载包含Lambda表达式功能的二进制文件。尝试从下载这里 http://jdk8.java.net/lambda/ 。
我记得在读lambda表达式分支在主JDK8构建中合并的邮件列表,但不确定它是否已完成。但我使用Lambda项目页面中的构建。
You need to download the binaries which contain the Lambda expressions feature. Try downloading from here http://jdk8.java.net/lambda/. I remember reading in the mailing list that the lambda expression branch is being merged in the main JDK8 build, but not sure if its been done. But I use the build from the Lambda project page.
这篇关于Lambda表达式在Java 8中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!