JavaFx:打开另外一个窗体并加载WebView

开发环境:jdk8openjfx11.0.2eclipse

1. 声明

当前内容主要为学习使用javafx实现,从一个窗口中打开另外一个窗口,在另外一个窗口中加载WebView并访问openjfx

主要内容:

  1. 创建Stage并打开
  2. 通过WebView访问openjfx

2. 基本demo

1. 使用SceneBuilder画出两个图
在这里插入图片描述
在这里插入图片描述
2.保存为fxml文件

第一个图的fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hy.java.gui.javafx.javafile.controller.OpenOtherWindowController">
   <children>
      <Pane prefHeight="408.0" prefWidth="600.0">
         <children>
            <Button layoutX="227.0" layoutY="185.0" mnemonicParsing="false" text="打开另外一个窗体"  onAction="#openOtherWindow"/>
            <Label layoutX="227.0" layoutY="36.0" text="JavaFx的基本测试">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
         </children>
      </Pane>
   </children>
</VBox>

第二个图的fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.web.WebView?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="503.0" prefWidth="818.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hy.java.gui.javafx.javafile.controller.SubWindowsController">
   <children>
      <Pane prefHeight="58.0" prefWidth="818.0">
         <children>
            <Text layoutX="-1.0" layoutY="22.0" strokeType="OUTSIDE" strokeWidth="0.0" text="欢迎使用测试工具" textAlignment="CENTER" wrappingWidth="820.30029296875">
               <font>
                  <Font size="24.0" />
               </font>
            </Text>
            <Button layoutX="678.0" layoutY="25.0" mnemonicParsing="false" onAction="#clickOnVisitWeb" text="访问" />
         </children>
      </Pane>
      <Pane prefHeight="447.0" prefWidth="818.0">
         <children>
            <WebView layoutY="-2.0" prefHeight="456.0" prefWidth="820.0" fx:id="browser" />
         </children>
      </Pane>
   </children>
</VBox>

3. 开始编写程序

1.基本的controller层

package com.hy.java.gui.javafx.javafile.controller;

import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class OpenOtherWindowController {

	@FXML public void openOtherWindow() throws IOException {
		Parent root = FXMLLoader.load(getClass().getResource("../toolsShow.fxml"));
		Stage stage = new Stage();
		Scene scene = new Scene(root, 1200, 800);
		stage.setTitle("Show Sub Window");
		System.out.println("6666666");
		stage.setScene(scene);

		stage.show();
		stage.setOnCloseRequest((event) -> {
			System.out.println("正在关闭当前的窗口..");
			stage.close();
		});
	}

}

package com.hy.java.gui.javafx.javafile.controller;

import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

public class SubWindowsController {
	@FXML WebView browser;

	public SubWindowsController() {
		System.out.println("browser=" + browser);
	}

	@FXML public void clickOnVisitWeb() {
		if (browser != null) {
			WebEngine engine = browser.getEngine();
			engine.load("https://openjfx.cn/");
			System.out.println("open a web ....");
		}
	}
}

2.开始编写入口类

package com.hy.java.gui.javafx.javafile;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class OpenOtherWindowTest extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("openOtherWindow.fxml"));
		Scene scene = new Scene(root, 500, 250);
		stage.setTitle("open other window");
		stage.setScene(scene);
		stage.setOnShown(new EventHandler() {

			@Override
			public void handle(Event event) {
				// TODO Auto-generated method stub
				System.out.println("event=" + event);

			}
		});
		stage.show();
	}
}

4. 开始测试

在这里插入图片描述

点击后

在这里插入图片描述
在这里插入图片描述
就是感觉没有自适应窗口,还是可以正常使用的(注意为第二个打开的添加关闭事件,关闭当前Stage,否则可能出现不退出的问题)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值