gpt4 book ai didi

JavaFX 按钮 单击可打开本地 HTML 自述文件

转载 作者:行者123 更新时间:2023-12-02 04:33:50 24 4
gpt4 key购买 nike

使用 JavaFX,我正在寻找一种打开本地 HTML 自述文件的方法。我知道这一定太简单了,因为我找不到任何东西。帮助链接位于工具栏中的 MenuItem 下。

最佳答案

我编写了一个最小的应用程序来完成您的需要。主要类是

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = FXMLLoader.load(getClass().getResource("/res/MenuWithHtmlReader.fxml"));

Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
launch(args);
}
}

FXML 是

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

<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.45" fx:controller="application.MainController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#onLoadHelpFile" text="Open Readme file" />
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<WebView fx:id="webView" prefWidth="500.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>

Controller 是:

package application;

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

public class MainController {

@FXML
WebView webView;

@FXML
public void onLoadHelpFile(ActionEvent event) {
System.out.println("onLoadHelpFile clicked" + webView);
WebEngine webEngine = webView.getEngine();
webEngine.load(getClass().getResource("/res/readme.html").toExternalForm());
}
}

帮助文件readme.html是

<html>
<body>
<p>Hello, this is your local friendly help file. Do as I say and you will be totally safe.</p>
<p>No pianos will plummet down onto your head.</p>
<p>Probably.</p>

</body>
</html>

我将fxml和html文件放在res文件夹中。我希望这会有所帮助。

关于JavaFX 按钮 单击可打开本地 HTML 自述文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31087101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com