WebView - 添加标签以显示状态
WebView - Adding a label to display status
我将以下代码与 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
的标签。
我尝试简单地在 controls
中添加一个 Label label = new Label("Marked: " + marked)
,但这不起作用。
关于如何向我的代码添加标签以显示 marked status
的任何建议?
感谢您的回复!
如果您使用实际代码向控件添加 Label
:
private boolean marked;
Label label = new Label("Marked: " + marked)
controls.getChildren().setAll(
highlightButton,
markedButton,
label
);
无论你以后改成marked
,都会一直显示Marked: false
。
如果您希望您的控件响应更改,JavaFX 具有 observable 属性,您可以阅读 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));
我将以下代码与 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
的标签。
我尝试简单地在 controls
中添加一个 Label label = new Label("Marked: " + marked)
,但这不起作用。
关于如何向我的代码添加标签以显示 marked status
的任何建议?
感谢您的回复!
如果您使用实际代码向控件添加 Label
:
private boolean marked;
Label label = new Label("Marked: " + marked)
controls.getChildren().setAll(
highlightButton,
markedButton,
label
);
无论你以后改成marked
,都会一直显示Marked: false
。
如果您希望您的控件响应更改,JavaFX 具有 observable 属性,您可以阅读 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));