从命令行将URL加载到WebEngine

问题描述

我正在构建一个简单的浏览器,并且希望能够从用户在命令行中提供的URL加载网站。我一直在尝试使其工作一段时间,但我无法弄清楚。这是相关代码

public class Week8Program extends Application {
    private Stage stage = null;
    private borderpane borderpane = null;
    private WebView view = null;
    private WebEngine webEngine = null;
    private TextField statusbar = null;

    
    /**
     * @param index - position of the argument in the args list.
     * @return The value of the command line argument.
     */
    private String getParameter( int index ) {
        Parameters params = getParameters();
        List<String> parameters = params.getRaw();
        return !parameters.isEmpty() ? parameters.get(index) : "";
    }

    /**
     * @return browser - a WebView container for the WebEngine.
     */
    private WebView makeHtmlView( ) {
        view = new WebView();
        webEngine = view.getEngine();
        return view;
    }

    private HBox makeStatusBar( ) {
        HBox statusbarPane = new HBox();                  //textfield at bottom containing URL the mouse
        statusbarPane.setPadding(new Insets(5,4,5,4)); //is hovering over
        statusbarPane.setSpacing(10);
        statusbarPane.setStyle("-fx-background-color: #336699;");
        statusbar = new TextField();
        HBox.setHgrow(statusbar,Priority.ALWAYS);
        statusbar.setEditable(false);
        statusbarPane.getChildren().addAll(statusbar);   
        return statusbarPane;
    }

    private HBox makeAddressBar () throws IOException {
        HBox addressBar = new HBox();                    //textfield at top for URL input

        Button backButton = new Button("<");             //back button
        backButton.setonAction(event -> {
            if ( webEngine.getHistory().getCurrentIndex() > 0){
                webEngine.getHistory().go(-1);
            }
        });

        Button forwardButton = new Button (">");         //forward button
        forwardButton.setonAction(event -> {
            webEngine.getHistory().go(+1);
        });

        File helpPage = new File("C:\\Users\\aberg\\IdeaProjects\\HTML5\\src\\HelpPage.html");
        Button helpButton = new Button ("?");            //help page
        helpButton.setonAction(event -> {
            webEngine.load(helpPage.toURI().toString());
        });

        addressBar.getChildren().add(backButton);
        addressBar.getChildren().add(forwardButton);
        TextField addressField = new TextField();
        addressBar.getChildren().add(addressField);
        addressBar.getChildren().add(helpButton);        //add all of the fields
        HBox.setHgrow(addressField,Priority.ALWAYS);
        addressField.setonAction((ActionEvent event)->{
            String url =addressField.getText();
            if(!url.startsWith("http:")){
                url = "http://" + url;
            }
            webEngine.load(url);                        //load URL in addressField
        });

        return addressBar;

    }

    
    /**
     
     * @param primaryStage
     */
    @Override
    public void start(Stage stage) throws IOException {
        this.stage = stage;
        borderpane pane = new borderpane();
        pane.setCenter(makeHtmlView());
        String arg = getParameter(0);
        webEngine.load(arg == null || arg.isEmpty() ? "http://www.mtu.edu" : arg);   //home page
        
        pane.setTop(makeAddressBar());
        pane.setBottom(makeStatusBar());
        webEngine.setonStatusChanged(e -> {             //changes text in status bar
            statusbar.setText(e.getData());

        });

        Scene scene = new Scene(pane,1024,768);
        stage.setScene(scene);
        stage.titleproperty().bind(view.getEngine().titleproperty());
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
        }
    }

我确定这是一个简单的补充,但我无法弄清楚。如果有人可以帮助,将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...