gpt4 book ai didi

java - 从 JavaFX WebView 中的 Ace Editor 获取代码

转载 作者:行者123 更新时间:2023-11-29 07:40:24 26 4
gpt4 key购买 nike

我下载了 Ace editor并把它放在我的本地机器上。我正在使用 JavaFX 通过 WebView 加载它。一切都很好,但是我如何检索编写的代码? (到 Java String)

最佳答案

只需通过调用执行 Javascript editor.getValue()

String code = (String)webView.getEngine().executeScript("editor.getValue()");

完整示例:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class AceEditorExample extends Application {

@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
webView.getEngine().load(getClass().getResource("Ace.html").toExternalForm());
Button saveButton = new Button("Save");
saveButton.setOnAction(e -> {
String code = (String) webView.getEngine().executeScript("editor.getValue()");
System.out.println(code);
});

BorderPane.setAlignment(saveButton, Pos.CENTER);
BorderPane.setMargin(saveButton, new Insets(10));
primaryStage.setScene(new Scene(new BorderPane(webView, null, null, saveButton, null), 800, 600));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

使用 Ace.html(例如):

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
</head>
<body>
<h2>Edit code</h2>
<div id="editor">

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class AceEditorExample extends Application {

@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
webView.getEngine().load(getClass().getResource("Ace.html").toExternalForm());
Button saveButton = new Button("Save");
saveButton.setOnAction(e -> {
String code = (String) webView.getEngine().executeScript("editor.getValue()");
System.out.println(code);
});

BorderPane.setAlignment(saveButton, Pos.CENTER);
BorderPane.setMargin(saveButton, new Insets(10));
primaryStage.setScene(new Scene(new BorderPane(webView, null, null, saveButton, null), 800, 600));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
</div>

<script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.getSession().setMode("ace/mode/java");
</script>
</body>
</html>

关于java - 从 JavaFX WebView 中的 Ace Editor 获取代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com