2017-07-27 27 views
-2

我做了一个java简单的网页浏览器。 我想知道我在特定的网页上访问了多少时间。 例如,如果我访问像stackoverflow.com这样的链接并在此停留几分钟,退出此网站后,我希望能够看到在控制台中花费的总时间。 继承人是我主要的Java文件 -如何获得我花费在网址上的时间?

package browser; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 


public class Browser extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("webfxml.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

,这是控权

package browser; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 


public class WebfxmlController implements Initializable { 
    @FXML 
    private TextField txt; 
    @FXML 
    private Button bt; 
    @FXML 
    private AnchorPane anc; 
    @FXML 
    private WebView webView; 

    /** 
    * Initializes the controller class. 
    */ 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
     WebEngine we=webView.getEngine(); 
     we.setJavaScriptEnabled(true); 

     EventHandler<ActionEvent> enter= new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent event) { 
       //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
       we.load(txt.getText().startsWith("http://")?txt.getText():"http://"+txt.getText()); 

      } 
     }; 
     txt.setOnAction(enter); 
     bt.setOnAction(enter); 

     we.locationProperty().addListener(new ChangeListener<String>() { 
       @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
        txt.setText(newValue); 
       } 
      }); 


    }  

    @FXML 
    private void handle(ActionEvent event) { 

     //txt.setText("Webview"); 
    } 

    }  

,这是webfxml-

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.web.*?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" fx:id="anc" prefHeight="560.0" prefWidth="784.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="browser.WebfxmlController"> 
    <children> 
     <TextField fx:id="txt" layoutX="14.0" layoutY="14.0" prefHeight="25.0" prefWidth="625.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="145.0" /> 
     <Button fx:id="bt" layoutX="654.0" layoutY="14.0" mnemonicParsing="false" onAction="#handle" prefHeight="25.0" prefWidth="68.0" text="Go" AnchorPane.rightAnchor="62.0" /> 
     <WebView fx:id="webView" layoutX="6.0" layoutY="50.0" prefHeight="509.0" prefWidth="784.0" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="6.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0" /> 
    </children> 
</AnchorPane> 
+1

添加监听到WebEngine –

+0

@Rollback的位置属性,您可以告诉我详情,请? –

回答

1

添加long startTime, endTime, druation;int startRecording = 0;

@FXML 
private TextField txt; 
@FXML 
private Button bt; 
@FXML 
private AnchorPane anc; 
@FXML 
private WebView webView; 

long startTime, endTime, duration; 
int startRecording = 0;//This helps you to not try to get the duration the first time the button is pressed. 

在当按压ButtonEventHandler开始记录时间。

EventHandler<ActionEvent> enter= new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) {     
      we.load(txt.getText().startsWith("http://")?txt.getText():"http://"+txt.getText());    
      startTime = System.nanoTime(); 
     } 
    }; 

locationProperty变化,记录结束时间。接下来,计算并显示持续时间。

we.locationProperty().addListener(new ChangeListener<String>() { 
      @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
       if(startRecording > 0) 
       { 
        endTime = System.nanoTime(); 
        duration = endTime - startTime; 
        if((duration/1000000000) >= 1) 
        { 
         System.out.println("duration: " + (duration/1000000000) + " seconds"); 
        } 
       } 
       startRecording++; 
       txt.setText(newValue); 
      } 
     }); 
+0

仅当位置属性发生变化时才有效!如果我滚动fb新闻饲料,那么它不显示适当的持续时间! :( –

+0

它符合你在你的问题中设置的要求,你说'例如,如果我访问像stackoverflow.com这样的链接,并在这里呆几分钟,退出本网站后,我希望能够看到在这里花费的总时间在控制台中。# – Sedrick

+0

编辑你的问题,并且更加精确。 – Sedrick

相关问题