现在我已经使用JavaFx完成了一个简单的Web浏览器。我设法做了一个backButton ....但是一个forwardButton我没有成功。此外,我想到了一个按钮,它将打开一个列表,其中包含在JavaFx浏览器中访问过的所有超链接(或链接)。如何让后退按钮工作?如何用一个按钮打开一个带有最后访问页面的hiperlinks(或链接)的窗口? (我想到了一个DefaultListModel和一个ArrayList,但我对你的意见不满意)。提前致谢 ! :)
这是我的代码:
backButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
public void handle(javafx.event.ActionEvent event) {
int sizeHistory = webEngine.getHistory().getEntries().size();
if(sizeHistory>1) {
Entry entry = webEngine.getHistory().getEntries().get(sizeHistory-2);
webEngine.load(entry.getUrl());
}
}
});
forwardButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
public void handle(javafx.event.ActionEvent event) {
int sizeHistory = webEngine.getHistory().getEntries().size();
if(sizeHistory>1) {
Entry entry = webEngine.getHistory().getEntries().get(sizeHistory);
System.out.println("URL hist.: " + entry.getUrl());
webEngine.load(entry.getUrl());
}
}
});
}
答案 0 :(得分:1)
我真的不明白你的后退和前进按钮。您计算为sizeHistory
的值只是访问过的页面总数(并存储在历史记录中)。当然,处理程序应该只是
backButton.setOnAction(e -> webEngine.getHistory().go(-1));
forwardButton.setOnAction(e -> webEngine.getHistory().go(1));
对于问题的其他部分,您可以创建一个ListView
来显示历史记录中的条目:
ListView<WebHistory.Entry> historyView = new ListView<>();
historyView.setItems(history.getEntries());
只需做一点工作,您就可以让列表中的单元格显示相应页面的标题:
historyView.setCellFactory(lv -> new ListCell<WebHistory.Entry>() {
@Override
public void updateItem(WebHistory.Entry entry, boolean empty) {
super.updateItem(entry, empty);
textProperty().unbind();
if (empty) {
setText(null);
} else {
textProperty().bind(entry.titleProperty());
}
}
});
并且您可以使用几个侦听器将选择绑定到历史记录的当前索引:
history.currentIndexProperty().addListener((obs, oldIndex, newIndex) -> {
if (newIndex.intValue() != historyView.getSelectionModel().getSelectedIndex()) {
historyView.getSelectionModel().clearAndSelect(newIndex.intValue());
}
});
historyView.getSelectionModel().selectedIndexProperty().addListener((obs, oldValue, newValue) -> {
if (newValue.intValue() != history.getCurrentIndex()) {
history.go(newValue.intValue() - history.getCurrentIndex());
}
});
SSCCE:
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Worker.State;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebBrowser extends Application {
private TextField textField ;
private WebView webView ;
@Override
public void start(Stage primaryStage) {
webView = new WebView();
webView.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == State.SUCCEEDED) {
primaryStage.setTitle(webView.getEngine().getTitle());
textField.setText(webView.getEngine().getLocation());
}
});
textField = new TextField("http://stackoverflow.com/questions/32802248/");
textField.setOnAction(e -> load());
load();
Button clearButton = new Button("Clear");
clearButton.setOnAction(e -> webView.getEngine().loadContent(""));
WebHistory history = webView.getEngine().getHistory() ;
Button backButton = new Button("Back");
backButton.disableProperty().bind(history.currentIndexProperty().lessThanOrEqualTo(0));
backButton.setOnAction(e ->
history.go(-1));
Button forwardButton = new Button("Forward");
forwardButton.disableProperty().bind(
history.currentIndexProperty().greaterThanOrEqualTo(Bindings.size(history.getEntries()).subtract(1)));
forwardButton.setOnAction(e ->
history.go(1));
HBox controls = new HBox(10, textField, backButton, clearButton, forwardButton);
HBox.setHgrow(textField, Priority.ALWAYS);
controls.setPadding(new Insets(10));
ListView<WebHistory.Entry> historyView = new ListView<>();
historyView.setItems(history.getEntries());
historyView.setCellFactory(lv -> new ListCell<WebHistory.Entry>() {
@Override
public void updateItem(WebHistory.Entry entry, boolean empty) {
super.updateItem(entry, empty);
textProperty().unbind();
if (empty) {
setText(null);
} else {
textProperty().bind(entry.titleProperty());
}
}
});
history.currentIndexProperty().addListener((obs, oldIndex, newIndex) -> {
if (newIndex.intValue() != historyView.getSelectionModel().getSelectedIndex()) {
historyView.getSelectionModel().clearAndSelect(newIndex.intValue());
}
});
historyView.getSelectionModel().selectedIndexProperty().addListener((obs, oldValue, newValue) -> {
if (newValue.intValue() != history.getCurrentIndex()) {
history.go(newValue.intValue() - history.getCurrentIndex());
}
});
BorderPane root = new BorderPane(webView, controls, historyView, null, null);
textField.setPadding(new Insets(5));
root.setPadding(new Insets(5));
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private void load() {
String url = makeUrl(textField.getText()) ;
webView.getEngine().load(url);
textField.setText(url);
}
private String makeUrl(String text) {
if (Pattern.matches("[a-zA-Z]+:.+", text)) {
return text ;
} else return "http://"+text ;
}
public static void main(String[] args) {
launch(args);
}
}