Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaFX时,书签和历史记录按钮不会显示在我的浏览器中_Java - Fatal编程技术网

使用JavaFX时,书签和历史记录按钮不会显示在我的浏览器中

使用JavaFX时,书签和历史记录按钮不会显示在我的浏览器中,java,Java,我正在用JavaFX制作一个网络浏览器,我觉得一切都很好。我运行了应用程序,现在历史记录和书签按钮将不会出现。我查看了代码,没有发现任何错误。我该如何解决这个问题 package javafxapplication3; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.application.Application; import javafx.scene.web.WebEngine; import java

我正在用JavaFX制作一个网络浏览器,我觉得一切都很好。我运行了应用程序,现在历史记录和书签按钮将不会出现。我查看了代码,没有发现任何错误。我该如何解决这个问题

package javafxapplication3;

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebHistory;

public class CreateAsIGo2 extends Application{
    public static void main(String[] args){
        launch(args);
    }
    public void start(Stage primaryStage){
        BorderPane ap = new BorderPane();
        BorderPane ap2 = new BorderPane();
        BorderPane ap3 = new BorderPane();

        Scene scene = new Scene(ap, 700, 700);
        Scene scene2 = new Scene(ap2, 700, 700);
        Scene scene3 = new Scene(ap3, 700, 700);

        VBox sp = new VBox();
        VBox sp2 = new VBox();
        VBox sp3 = new VBox();

        Button HistoryButton = new Button("History");
        Button BookmarksButton = new Button("Bookmarks");        
        Button RefreshButton = new Button("Refresh");
        Button BackButton = new Button("Back");
        Button BackToBrowser = new Button("Back to surfing the web");
        Button ForwardButton = new Button("Forward");

        TextField tf = new TextField();
        tf.setPromptText("URL");

        WebView browser = new WebView();

        WebEngine webEngine = browser.getEngine();
        webEngine.load("http://www.google.com");
        webEngine.setJavaScriptEnabled(true);

        WebHistory history = webEngine.getHistory();



        HistoryButton.setOnAction(e -> primaryStage.setScene(scene2));
        BookmarksButton.setOnAction(e -> primaryStage.setScene(scene3));
        RefreshButton.setOnAction(e -> webEngine.reload());        
        BackButton.setOnAction(e -> webEngine.executeScript("history.back()"));        
        BackToBrowser.setOnAction(e -> primaryStage.setScene(scene));        
        ForwardButton.setOnAction(e -> webEngine.executeScript("history.forward()"));

        tf.setOnKeyPressed((KeyEvent ke) -> {
            KeyCode key = ke.getCode();
            if(key == KeyCode.ENTER){
                webEngine.load("http://" + tf.getText());
            }
        });

        sp.getChildren().addAll(HistoryButton, BookmarksButton, RefreshButton, BackButton, ForwardButton);
        sp2.getChildren().addAll(BookmarksButton, BackToBrowser);
        sp3.getChildren().addAll(HistoryButton, BackToBrowser);

        ap.setRight(sp);
        ap2.setRight(sp2);
        ap3.setRight(sp3);
        ap.setTop(tf);
        ap.setCenter(browser);

        browser.setPrefSize(700, 700);
        primaryStage.setTitle("JTG Browser Alpha");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
见:

如果程序将子节点添加到父节点(包括组、区域等),并且该节点已经是其他父节点的子节点或场景的根节点,则该节点将自动(静默)从其前父节点中移除

您正在将您的
历史按钮
(和其他按钮)添加到不同的场景中,这样做时,它们将自动从以前的场景中删除。如果希望按钮实例在每个场景中都可见,则需要创建新的按钮实例

旁白:最好遵循Java命名约定,例如
historyButton
,而不是
historyButton