我有一台运行Windows XP SP3 32位系统的虚拟机,在此机器上,我从此处安装了Java SE JDK 8版本为b44 Developer Preview。
我还安装了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)
我尝试运行以下程序(摘自这里):
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();
}
}
程序按预期编译并运行。我按照那篇文章中的说明,用以下代码替换了按钮事件处理代码:
button.setOnAction((ActionEvent event)->
{
ToggleButton source = (ToggleButton) event.getSource();
if (source.isSelected())
{
btnText.set("Clicked!");
}
else
{
btnText.set("Click!");
}
});
编译时,我在以下代码行(button.setOnAction((ActionEvent event)->
)处遇到了如下错误:
> lambda expressions are not supported in -source 1.8
> (use -source 8 or higher to enable lambda expressions)
我添加了参数-source 8
,但什么也没有改变。
我想要检查Java 8中lambda表达式的功能性,为什么它不起作用?
javac
命令行,导致无效的参数仍然存在吗? - Marko Topolnik