簡體   English   中英

如何獲取我在網址中花費的時間?

[英]How to get the time that I spent in a url?

我做了一個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>

添加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.

EventHandler按下Button時開始錄制時間。

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);
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM