javaFX系列之web組件:史上最簡單的javaFX瀏覽器實現(web component組件)

前言

本章接着之前的根據組件學習javaFX的系列教程,本章爲javaFX視頻播放器的簡單實現。

javaFX結構組件

參考

本章有且只參考了oracle javaFX官方文檔:
https://docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-architecture.htm#JFXST110

功能實現

  1. web網頁瀏覽
  2. 後退
  3. 前進
  4. 主頁
  5. 刷新
  6. 地址欄

代碼實現

/**
 * javaFX最簡單的網頁瀏覽器
 * @author eguid
 *
 */
public class JavaFxWebView extends Application {

	Scene scene;
	
	WebEngine webEngine;
	
	String url;
	
	public JavaFxWebView() {

		Console.log("創建" + this.getClass().getSimpleName() + "對象,順序:2,當前線程: " + Thread.currentThread().getName());
	}

	@Override
	public void init() throws Exception {
		url=getParam("url");
		// 與對象創建,start,stop方法不屬於同一個線程
		Console.log("執行了init初始化" + this.getClass().getSimpleName() + ",順序:3,當前線程: " + Thread.currentThread().getName());
		// init方法是由javaFX-launcher線程執行的,也就是說,init中的初始化的數據,在start中使用可能會發生空指針
		super.init();

	}

	@Override
	public void stop() throws Exception {
		// TODO Auto-generated method stub
		Console.log("執行stop銷燬" + this.getClass().getSimpleName() + "對象,順序:5,當前線程: " + Thread.currentThread().getName());
		super.stop();
	}
	/**
	 * 獲取輸出參數
	 * @param key
	 * @return
	 */
	private String getParam(String key) {
		Map<String, String> params=getParameters().getNamed();
		return params.get(key);
	}
	
	public void redirect(String url) {
		this.url=url;
		webEngine.load(url);
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {

		BorderPane root = new BorderPane();
		//瀏覽器
		WebView browser = new WebView();
		browser.autosize();
		webEngine = browser.getEngine();
		
		//初始化打開的地址
		redirect(url);
		
		this.scene = new Scene(root, 800, 600);
		
		root.getChildren().add(browser);
				
		//操作面板	
		HBox mb = new HBox();
		mb.setAlignment(Pos.CENTER);//設置對齊方式:中間對齊
		mb.setPadding(new Insets(5, 10, 5, 10));//設置內邊距
		BorderPane.setAlignment(mb, Pos.TOP_LEFT);//設置對其方式:中間對齊
		
		mb.setStyle("-fx-background-color:#FFF");
		Button backButton = new Button(" < ");
		backButton.setCursor(Cursor.HAND);
		backButton.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				Console.log("後退");
				WebHistory history =webEngine.getHistory();
				int cuIndex=history.getCurrentIndex();
				Console.log("當前索引:"+cuIndex);
				if(cuIndex>0) {
					history.go(-1);
				}
				Console.log("返回上一頁之後當前索引:"+history.getCurrentIndex());
			}
		});
		mb.getChildren().add(backButton);
		
		Button forwardButton = new Button(" > ");
		forwardButton.setCursor(Cursor.HAND);
		forwardButton.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				Console.log("前進");
				WebHistory history =webEngine.getHistory();
				int cuIndex=history.getCurrentIndex();
				Console.log("當前索引:"+cuIndex);
				
				history.go(1);
			}
		});
		mb.getChildren().add(forwardButton);
		
		Button refreshButton = new Button(" 刷新 ");
		refreshButton.setCursor(Cursor.HAND);
		refreshButton.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				Console.log("刷新");
				webEngine.load(url);
			}
		});
		mb.getChildren().add(refreshButton);
		
		Label spacer1 = new Label(" ");
		mb.getChildren().add(spacer1);
		//主頁按鈕
		Button stopButton = new Button(" 主頁 ");
		stopButton.setCursor(Cursor.HAND);
		stopButton.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				redirect("http://blog.csdn.net/eguid_1");
			}
		});
		mb.getChildren().add(stopButton);
		
		Label spacer2 = new Label(" ");
		mb.getChildren().add(spacer2);
		
		TextField inputValue=new TextField("");
		inputValue.setCursor(Cursor.TEXT);
		inputValue.setOnKeyPressed(new EventHandler<KeyEvent>() {

			@Override
			public void handle(KeyEvent event) {
				Console.log("鍵盤按鍵:"+event.getCode());
				if(event.getCode().getName().equals(KeyCode.ENTER.getName())) {
					Console.log("是否enter按鍵");
					String inputText=inputValue.getText();
					Console.log("值"+ inputText);
					if(inputText!=null&&!inputText.isEmpty()) {
						redirect(inputText.indexOf("http")==0?inputText:"http://"+inputText);
					}
				}
				
			}
		});
		mb.getChildren().add(inputValue);
		
//		root.getChildren().add(mb);
		root.setTop(mb);
		
		
		Console.log(this.getClass().getSimpleName() + "開始顯示窗口,順序:4,當前線程:" + Thread.currentThread().getName());
		primaryStage.setTitle("eguid最簡單的javaFX瀏覽器");
		primaryStage.setScene(scene);
		primaryStage.setAlwaysOnTop(true);

		primaryStage.setFullScreen(false);// 全屏
		primaryStage.sizeToScene();
		primaryStage.requestFocus();
		primaryStage.setResizable(true);

		primaryStage.show();
	}
}

運行

public static void main(String[] args) {
		launch("--url=http://eguid.blog.csdn.net");
	}

效果演示

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章