Ich machte einen Java-einfachen Webbrowser. Ich möchte wissen, wie viel Zeit ich in bestimmten Webseiten besucht habe. Zum Beispiel, wenn ich einen Link wie stackoverflow.com besuche und hier einige Minuten blieb, nachdem ich diese Seite verlassen habe, möchte ich die Gesamtzeit hier in der Konsole sehen können. Heres ist mein Haupt-Java-File-Wie bekomme ich die Zeit, die ich in einer URL verbracht habe?
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);
}
}
und dies ist der Controller-
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");
}
}
und dies ist der 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>
einen Zuhörer auf die Location-Eigenschaft des bitte WebEngine –
@rollback können Sie mir sagen, in Details hinzufügen? –