java - 如何在 javafx 浏览器中操作链接

标签 java netbeans web browser javafx

        //process history
        final WebHistory history = webEngine.getHistory();

        history.getEntries().addListener(new 
            ListChangeListener<WebHistory.Entry>(){
                @Override
                public void onChanged(Change<? extends Entry> c) {
                    c.next();
                    for (Entry e : c.getRemoved()) {
                        comboBox.getItems().remove(e.getUrl());

                    }

                    for (Entry e : c.getAddedSubList()) {
                        comboBox.getItems().add(e.getUrl());
                    }
                        System.out.println(webEngine.getLocation());
                    if(webEngine.getLocation().matches("http://www.google.co.in/")){}
                    else if(webEngine.getLocation().contains("http://in.yahoo.com/")){}
                    else{

          browser.setOnMouseClicked(new EventHandler<MouseEvent>(){

          @Override
          public void handle(MouseEvent arg0) {

                String[] manipulateurl= {
                                         "http://www.gmail.com",
                                         "http://www.flipkart.com"};
                String temp=address.getText();


               if (manipulatedindex>1)
                   manipulatedindex=0;

               temp=manipulateurl[manipulatedindex];
               System.out.println(manipulateurl[manipulatedindex]+"      "+temp);
               address.setText(temp);


                webEngine.load(temp);
                manipulatedindex++;

}

    });}



//                         webEngine.getLocation()
                }
        });

        //set the behavior for the history combobox               
        comboBox.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ev) {
                int offset =
                        comboBox.getSelectionModel().getSelectedIndex()
                        - history.getCurrentIndex();
                history.go(offset);
            }
        });



        // process page loading
        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                @Override
                public void changed(ObservableValue<? extends State> ov,
                    State oldState, State newState) {
                    toolBar.getChildren().removeAll(showAll, hideAll);    
                    if (newState == State.SUCCEEDED) {
                            JSObject win = 
                                (JSObject) webEngine.executeScript("window");
                            win.setMember("app", new JavaApp());
                            if (needForumButtons) {
                                toolBar.getChildren().addAll(showAll, hideAll);
                            }
                        }
                    }
                }
        );

        // load the home page  
        String x="http://www.google.com";
    webEngine.load(x);
        //add components

        getChildren().add(toolBar);


        getChildren().add(browser);
    }

    // JavaScript interface object
    private class JavaApp {

        public void exit() {
            Platform.exit();
        }
    }

    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        double tbHeight = toolBar.prefHeight(w);
        layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
        layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
    }

    @Override
    protected double computePrefWidth(double height) {
        return 750;
    }

    @Override
    protected double computePrefHeight(double width) {
        return 600;
    }
}

这是我的代码。有人可以帮我操纵浏览器上的链接吗?例如,当我点击 google.com 中的“广告程序”时,它应该转到 (propams.com) 以及我点击过的网址

感谢您提前帮助我。

最佳答案

在你的代码中,你应该首先尝试理解历史是如何在浏览器中显示的,如果你弄清楚了,那么我想你只需要编写一个简单的 if else 逻辑来操作链接到你想要的任何地方所以首先尝试显示历史我想这应该可以解决问题

关于java - 如何在 javafx 浏览器中操作链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15794516/

相关文章:

javascript - 如何像facebook wall一样显示输入的url的页面预览?

java - 二维数组不以矩阵格式打印值

java - 与新类不兼容的类型?

java - 使用 Java JADE 创建多个代理

java - 如果 C++ 头文件包含私有(private)成员变量声明,如何将其视为类的 "interface"?

java - 在 NetBeans 快捷方式中注释/取消注释代码块

javascript - ReactJS 不使用 && 显示数据

javascript - 为什么客户端认证不好?

php - 如何选择 laravel Controller 以使用 netbeans 进行测试

java - 如何使用netbeans 7开发需要netbeans平台6x的应用程序?