如何获得我在网址上花费的时间?

时间:2017-07-27 20:26:25

标签: java javafx java-8 webbrowser-control scenebuilder

我制作了一个简单的Java浏览器。 我想知道我在特定网页上访问了多少时间。 例如,如果我访问像stackoverflow.com这样的链接并在这里停留几分钟,退出此站点之后我希望能够在控制台中看到这里花费的总时间。 Heres是我的主要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 个答案:

答案 0 :(得分: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.
  

在按下EventHandler的{​​{1}}开始录制时间。

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