我做了一个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>
添加监听到WebEngine –
@Rollback的位置属性,您可以告诉我详情,请? –