gpt4 book ai didi

java - WebView - 添加标签以显示状态

转载 作者:行者123 更新时间:2023-12-02 05:03:42 24 4
gpt4 key购买 nike

我使用 javaFxjava 8 中使用以下代码。

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;

@SuppressWarnings("all")
public class Highlighter extends Application {

private boolean marked;


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

@Override
public void start(Stage primaryStage) {
final WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
engine.load("http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html");

final TextField searchField = new TextField("light");
searchField.setPromptText("Enter the text you would like to highlight and press ENTER to highlight");
searchField.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if (engine.getDocument() != null) {
highlight(
engine,
searchField.getText()
);
}
}
});

final Button highlightButton = new Button("Highlight");
highlightButton.setDefaultButton(true);
highlightButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
searchField.fireEvent(new ActionEvent());
}
});
final Button markedButton = new Button("Mark it");
markedButton.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent actionEvent) {
marked = true;
}
});
markedButton.setCancelButton(true);

HBox controls = new HBox(10);
controls.getChildren().setAll(
highlightButton,
markedButton
);

VBox layout = new VBox(10);
layout.getChildren().setAll(searchField, controls, webView);
searchField.setMinHeight(Control.USE_PREF_SIZE);
controls.setMinHeight(Control.USE_PREF_SIZE);

controls.disableProperty().bind(webView.getEngine().getLoadWorker().runningProperty());
searchField.disableProperty().bind(webView.getEngine().getLoadWorker().runningProperty());

primaryStage.setScene(new Scene(layout));
primaryStage.show();

webView.requestFocus();
}

private void highlight(WebEngine engine, String text) {
engine.executeScript("$('body').removeHighlight().highlight('" + text + "')");
}

}

我的问题是我想添加一个标签来显示页面的标记状态

我尝试简单地将 Label label = new Label("Marked: "+ Marked) 添加到控件,但这不起作用。

有什么建议可以让我在代码中添加标签来显示标记状态吗?

感谢您的回复!

最佳答案

如果您使用实际代码向控件添加标签:

private boolean marked;

Label label = new Label("Marked: " + marked)
controls.getChildren().setAll(
highlightButton,
markedButton,
label
);

无论您之后是否更改 marked,它都会始终显示 Marked: false

如果您希望控件响应更改,JavaFX 具有可观察的属性,您可以阅读 here .

因此,您可以用包装 boolean 值的属性替换 boolean 基元:

private final BooleanProperty marked=new SimpleBooleanProperty();

创建标签:

    Label label=new Label("Marked: "+marked.get());
HBox controls = new HBox(10);
controls.setAlignment(Pos.CENTER_LEFT);
controls.getChildren().setAll(
highlightButton,
markedButton,
label
);

更改markedButton的事件:

    markedButton.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent actionEvent) {
marked.set(true);
}
});

(这只会工作一次,因为目前您还没有实现将 marked 再次重置为 false 的方法)

最后,为 marked 属性中的任何更改添加监听器:

    marked.addListener(new ChangeListener<Boolean>() {

@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
label.setText("Marked: "+newValue);
}
});

除了监听器之外,您还可以使用绑定(bind):

Label label=new Label();
label.textProperty().bind(Bindings.concat("Marked: ").concat(marked));

关于java - WebView - 添加标签以显示状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002138/

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