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

时间:2013-04-03 17:54:28

标签: 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)我点击的网址

感谢您提前帮助我。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您应首先尝试了解历史记录在浏览器中的显示方式,如果您想出来的话我猜您只需编写一个简单的if else逻辑来操作链接到您想要的位置,因此首先尝试显示历史记录我想这应该做的伎俩