我正在使用java 8将以下代码与javaFx一起使用。

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

}


我的问题是我想添加显示页面marked status的标签。

我尝试仅将Label label = new Label("Marked: " + marked)添加到controls,但这不起作用。

关于如何在代码中添加标签以显示marked status的任何建议?

感谢您的答复!

最佳答案

如果使用实际代码向控件添加Label

private boolean marked;

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


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

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

因此,您可以使用以下包装布尔值的属性来替换布尔基元:

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


除了监听器,您还可以使用Bindings

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

10-06 05:02