实现了基本的浏览器操作;测试了本地html加载

This commit is contained in:
free will committed 2020-10-15 22:04:45 +08:00
commit a3febe6106
82 files changed
+328

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
.git/
.gradle/
.idea/
out/
+44
View File
@@ -0,0 +1,44 @@
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.5'
}
apply plugin: 'java'
apply plugin: 'org.openjfx.javafxplugin'
group 'PKUSZ'
version '0.1'
sourceCompatibility = 11
targetCompatibility = 11
//指定编译编码
tasks.withType(JavaCompile){
options.encoding="UTF-8"
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
// mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile fileTree(dir: 'lib',includes: ['*jar'])
compile 'com.blankj:utilcode:1.10.0'
}
javafx {
version = "11.0.2"
modules = [ 'javafx.controls','javafx.fxml','javafx.web' ]
}
jar{
manifestContentCharset 'utf-8'
metadataCharset 'utf-8'
manifest{
attributes "Main-Class": "cn.minbrowser.Main"
}
from{
configurations.compile.collect{it.isDirectory()?it:zipTree(it)}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 343 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

+2
View File
@@ -0,0 +1,2 @@
rootProject.name = 'MINBrowser'
+13
View File
@@ -0,0 +1,13 @@
package cn.minbrowser;
/*
* @author wefree
*/
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(MainApp.class);
}
}
+72
View File
@@ -0,0 +1,72 @@
package cn.minbrowser;
/*
* @author wefree
*/
import cn.minbrowser.view.NavigationBarController;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import java.io.IOException;
public class MainApp extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
//设置关闭监听,确保在关闭stage后关闭子线程
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
this.primaryStage = primaryStage;
// 设置平台自带标题栏的名称
this.primaryStage.setTitle("MINBrowser");
// 设置平台自带标题栏的图标
this.primaryStage.getIcons().add(new Image("file:resources/images/logo_mini.png"));
// 设置平台stage显示格式
this.primaryStage.initStyle(StageStyle.DECORATED);// 带标题栏,默认格式
// this.primaryStage.setResizable(false);
showNavigationBarOverview();
}
// 展示浏览器导航界面
public void showNavigationBarOverview() {
try {
// 加载overview
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getClassLoader().getResource("fxml/NavigationBarOverview.fxml"));
AnchorPane navigationBar = (AnchorPane) loader.load();
// 设置scene
Scene scene = new Scene(navigationBar);
primaryStage.setScene(scene);
// 传参给控制器
NavigationBarController barController = loader.getController();
barController.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
// 展示登录界面
public void showLoginOverview() {
}
}
@@ -0,0 +1,139 @@
package cn.minbrowser.view;
/*
* @author wefree
*/
import cn.minbrowser.MainApp;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.WebView;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
public class NavigationBarController implements Initializable {
private MainApp mainApp;
@FXML
private Button backButton;
@FXML
private Button progressButton;
@FXML
private Button refreshButton;
@FXML
private TextField urlText;
@FXML
private ImageView userPhoto;
@FXML
private WebView mainWebView;
public NavigationBarController(){
}
@FXML
private void initialize() {
//在fxml被载入时候被自动调用
this.userPhoto.setImage(new Image("file:resources/images/user.png"));
// webview 加入监听器:状态改变后,自动把新的url回显到文本输入框
mainWebView.getEngine().getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue ov, Worker.State oldValue, Worker.State newValue) {
System.out.println(mainWebView.getEngine().getLocation());
urlText.setText(mainWebView.getEngine().getLocation());
}
});
}
// 后退
@FXML
private void backUrl(){
if (mainWebView.getEngine().getHistory().getCurrentIndex() > 0) {
mainWebView.getEngine().getHistory().go(-1);
}
}
// 前进
@FXML
private void progressUrl(){
if(mainWebView.getEngine().getHistory().currentIndexProperty().getValue()
< mainWebView.getEngine().getHistory().getEntries().size() - 1){
mainWebView.getEngine().getHistory().go(1);
}
}
// 刷新
@FXML
private void refreshUrl(){
mainWebView.getEngine().reload();
}
// 跳转/加载
@FXML
private void loadUrl(KeyEvent keyEvent) throws MalformedURLException {
if(keyEvent.getCode()== KeyCode.ENTER){
String url=urlText.getText().trim();
if(isPrefix(url)){
// 本地加载
String filepath=getRootPath()+"/webcache"+url+"/index.html";
File file=new File(filepath);
URL fileUrl=file.toURI().toURL();
System.out.println(filepath);
System.out.println(fileUrl.toString());
mainWebView.getEngine().load(fileUrl.toString());
}else{
// http加载
mainWebView.getEngine().load(urlText.getText().trim());
}
}
}
// 获取项目根目录路径
private static String getRootPath() {
File directory = new File("");// 参数为空
String path = "";
try {
path = directory.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("客户端根目录:" + path);
return path;
}
// 判断一个字符串是否是标识地址
private boolean isPrefix(String url){
if(url.startsWith("/")&&(url.length()>1)){
return true;
}else{
return false;
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
/*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="768.0" prefWidth="1366.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.minbrowser.view.NavigationBarController">
<children>
<ToolBar prefHeight="44.0" prefWidth="1366.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<Button fx:id="backButton" mnemonicParsing="false" onAction="#backUrl" text="后退" textAlignment="CENTER" />
<Button fx:id="progressButton" mnemonicParsing="false" onAction="#progressUrl" text="前进 " textAlignment="CENTER" />
<Button fx:id="refreshButton" mnemonicParsing="false" onAction="#refreshUrl" text="刷新" textAlignment="RIGHT" />
<TextField fx:id="urlText" onKeyPressed="#loadUrl" prefHeight="30.0" prefWidth="1110.0" />
</items>
</ToolBar>
<ImageView fx:id="userPhoto" fitHeight="40.0" fitWidth="40.0" layoutX="1152.0" layoutY="-8.0" pickOnBounds="true" preserveRatio="true" AnchorPane.rightAnchor="30.0" AnchorPane.topAnchor="1.0">
<image>
<Image url="@../../../../resources/images/user.png" />
</image>
</ImageView>
<WebView fx:id="mainWebView" layoutX="22.0" layoutY="60.0" prefHeight="692.0" prefWidth="1282.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="50.0" />
</children>
</AnchorPane>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="fxml.common.HomePageOverview"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>
+10
View File
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestTitle</title>
</head>
<body style="background-image: url(test.jpg)">
Hello, MINBrowser!
</body>
</html>
Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB