JShell 샘플 표현식을 구현하는 데 사용되는 대화형 도구입니다. JavaFX 를 사용하여 프로그래밍 방식으로 JShell을 구현할 수 있습니다. 그런 다음 아래 나열된 Java 프로그램에서 몇 가지 패키지를 가져와야 합니다.
import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet;
아래 예에서는 샘플 Java FX 애플리케이션을 구현했습니다. 텍스트 필드에 다른 값을 입력합니다. "평가를 누릅니다. " 버튼. 목록에 해당 데이터 유형의 값이 표시됩니다.
예시
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.List;
import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;
import jdk.jshell.VarSnippet;
public class JShellFXTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
JShell shell = JShell.builder().build();
TextField textField = new TextField();
Button evalButton = new Button("eval");
ListView<String> listView = new ListView<>();
evalButton.setOnAction(e -> {
List<SnippetEvent> events = shell.eval(textField.getText());
events.stream().map(event -> convert(event)).filter(s -> s != null).forEach(s -> listView.getItems().add(s));
});
BorderPane pane = new BorderPane();
pane.setTop(new HBox(textField, evalButton));
pane.setCenter(listView);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
public static String convert(SnippetEvent e) {
if(e.snippet() instanceof VarSnippet) {
return ((VarSnippet) e.snippet()).typeName() + " " + ((VarSnippet) e.snippet()).name() + " " + e.value();
}
return null;
}
public static void main(String[] args) {
launch();
}
} 출력
