0

我将以下代码与java 8using一起使用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页面的标签。

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

有什么建议可以在我的代码中添加标签以显示marked status

感谢您的回复!

4

1 回答 1

2

如果您Label使用实际代码添加到控件:

private boolean marked;

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

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

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

所以你可以用这个包装布尔值的属性替换布尔原语:

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));
于 2015-01-17T18:48:04.090 回答