Lambda 表达式在 Java 8 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11176936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:04:46  来源:igfitidea点击:

Lambda expressions don't work in Java 8?

javalambdajavafx-2java-8

提问by Radu Murzea

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.

我有一台运行 Windows XP SP3 32 位的虚拟机。在这台机器上,我从这里安装了 Java SE JDK 8 build b44 Developer Preview 。

I also installed the JavaFX 2.1 SDK.

我还安装了 JavaFX 2.1 SDK。

It works fine:

它工作正常:

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!");
    }
});

When compiling, I get the following error (on the line button.setOnAction((ActionEvent event)->):

编译时,我收到以下错误(在线button.setOnAction((ActionEvent event)->):

> lambda expressions are not supported in -source 1.8
> (use -source 8 or higher to enable lambda expressions)

I added the argument -source 8, nothing changed.

我添加了参数-source 8,没有任何改变。

All I wanted was to check the lambda expressions functionality in Java 8. Why doesn't it work ?

我想要的只是检查 Java 8 中的 lambda 表达式功能。为什么它不起作用?

回答by MohamedSanaulla

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 表达式功能的二进制文件。尝试从这里下载http://jdk8.java.net/lambda/。我记得在邮件列表中读到 lambda 表达式分支正在合并到主 JDK8 构建中,但不确定是否已完成。但我使用来自 Lambda 项目页面的构建。

回答by jewelsea

As an alternative to the http://jdk8.java.net/lambda/releases, there are also JDK8 and JRE8 snapshot releases available in dmg, exe installer and tar.gz form from http://jdk8.java.net/download.html.

作为http://jdk8.java.net/lambda/版本的替代,还有 JDK8 和 JRE8 快照版本以 dmg、exe 安装程序和来自http://jdk8.java.net/download 的tar.gz 形式提供html的

These jdk8 developer snapshots also now include lambda (and javafx8) functionality. As of this writing, both lambda and javafx for jdk8 are still in feature and bug fix development mode.

这些 jdk8 开发人员快照现在还包括 lambda(和 javafx8)功能。在撰写本文时,用于 jdk8 的 lambda 和 javafx 仍处于功能和错误修复开发模式。

I have found the http://jdk8.java.net/download.htmlweekly builds useful and useable for development (programs using them will occasionally crash on my mac, so it's definitely not something you would want to deploy to production even if the oracle license for the builds permitted that, which they don't).

我发现http://jdk8.java.net/download.html每周构建有用且可用于开发(使用它们的程序偶尔会在我的 mac 上崩溃,所以它绝对不是你想要部署到生产的东西,即使构建的 oracle 许可证允许这样做,而他们不允许)。

If you want the absolute latest bleeding edge or customized jdk builds, you can build your own using obuildfactory. Such builds use an OpenJDK licensing model rather than an Oracle JDK licensing model, which may be preferable for some development projects, though, personally, I stick with Oracle JDK builds when I can.

如果您想要绝对最新的前沿或自定义 jdk 构建,您可以使用obuildfactory构建您自己的。此类构建使用 OpenJDK 许可模型而不是 Oracle JDK 许可模型,这对于某些开发项目可能更可取,但就我个人而言,我尽可能坚持使用 Oracle JDK 构建。