日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JavaFX UI控件教程(十九)之Hyperlink

發布時間:2023/12/3 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaFX UI控件教程(十九)之Hyperlink 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

翻譯自? ?Hyperlink

本章介紹Hyperlink用于將文本格式化為超鏈接的控件。

所述Hyperlink類表示另一種類型的Labeled控制。圖18-1演示了默認超鏈接實現的三種狀態。

圖18-1超鏈接控制的三種狀態

?

創建超鏈接

示例18-1中顯示了生成超鏈接的代碼片段。

例18-1典型的超鏈接

Hyperlink link = new Hyperlink(); link.setText("http://example.com"); link.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {System.out.println("This link is clicked");} });

該setText實例方法定義了超鏈接的文本標題。因為Hyperlink類是類的擴展,所以Labeled可以為超鏈接標題設置特定的字體和文本填充。該setOnAction方法設置特定操作,只要單擊超鏈接就會調用該操作,類似于此方法對Button控件的工作方式。在例18-1中,此操作僅限于打印字符串。但是,在您的應用程序中,您可能希望實現更常見的任務。

鏈接本地內容

圖18-2中的應用程序從本地目錄呈現圖像。

圖18-2查看圖像

查看例18-2中顯示的此應用程序的源代碼。

示例18-2使用超鏈接查看圖像

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.Stage;public class Main extends Application {final static String[] imageFiles = new String[]{"product.png","education.png","partners.png","support.png"};final static String[] captions = new String[]{"Products","Education","Partners","Support"};final ImageView selectedImage = new ImageView();final ScrollPane list = new ScrollPane();final Hyperlink[] hpls = new Hyperlink[captions.length];final Image[] images = new Image[imageFiles.length];public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {Scene scene = new Scene(new Group());stage.setTitle("Hyperlink Sample");stage.setWidth(300);stage.setHeight(200);selectedImage.setLayoutX(100);selectedImage.setLayoutY(10);for (int i = 0; i < captions.length; i++) {final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);final Image image = images[i] = new Image(getClass().getResourceAsStream(imageFiles[i]));hpl.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {selectedImage.setImage(image);}});}final Button button = new Button("Refresh links");button.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {for (int i = 0; i < captions.length; i++) {hpls[i].setVisited(false);selectedImage.setImage(null);}}});VBox vbox = new VBox();vbox.getChildren().addAll(hpls);vbox.getChildren().add(button);vbox.setSpacing(5);((Group) scene.getRoot()).getChildren().addAll(vbox, selectedImage);stage.setScene(scene);stage.show();} }

此應用程序Hyperlink在for循環中創建四個對象。setOnAction調用每個超鏈接的方法定義用戶單擊特定超鏈接時的行為。在這種情況下,images為selectedImage變量設置數組中的相應圖像。

當用戶單擊超鏈接時,它將被訪問。您可以使用類的setVisited方法Hyperlink刷新鏈接。例18-3中的代碼片段完成了這項任務。

示例18-3刷新HyperlInks

final Button button = new Button("Refresh links"); button.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {for (int i = 0; i < captions.length; i++) {hpls[i].setVisited(false);selectedImage.setImage(null);}} });

單擊時,“刷新鏈接”按鈕會將所有超鏈接帶到未訪問狀態,如圖18-3所示。

圖18-3未訪問的超鏈接

由于Hyperlink該類是類的擴展,因此Labeled您不僅可以指定文本標題,還可以指定圖像。下一節中提供的應用程序使用文本標題和圖像來創建超鏈接和加載遠程HTML頁面。

?

鏈接遠程內容

您可以通過WebView在應用程序場景中嵌入瀏覽器來在JavaFX應用程序中呈現HTML內容。該WebView組件提供基本的網頁瀏覽功能。它呈現網頁并支持用戶交互,例如導航鏈接和執行JavaScript命令。

研究例18-4中應用程序的源代碼。它創建了四個帶有文本標題和圖像的超鏈接。單擊超鏈接時,相應的值將作為URL傳遞給嵌入式瀏覽器。

示例18-4加載遠程網頁

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage;public class Main extends Application {final static String[] imageFiles = new String[]{"product.png","education.png","partners.png","support.png"};final static String[] captions = new String[]{"Products","Education","Partners","Support"};final static String[] urls = new String[]{"http://www.oracle.com/us/products/index.html","http://education.oracle.com/","http://www.oracle.com/partners/index.html","http://www.oracle.com/us/support/index.html"};final ImageView selectedImage = new ImageView();final Hyperlink[] hpls = new Hyperlink[captions.length];final Image[] images = new Image[imageFiles.length]; public static void main(String[] args){launch(args);}@Overridepublic void start(Stage stage) {VBox vbox = new VBox();Scene scene = new Scene(vbox);stage.setTitle("Hyperlink Sample");stage.setWidth(570);stage.setHeight(550);selectedImage.setLayoutX(100);selectedImage.setLayoutY(10);final WebView browser = new WebView();final WebEngine webEngine = browser.getEngine();for (int i = 0; i < captions.length; i++) {final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);final Image image = images[i] =new Image(getClass().getResourceAsStream(imageFiles[i]));hpl.setGraphic(new ImageView (image));hpl.setFont(Font.font("Arial", 14));final String url = urls[i];hpl.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {webEngine.load(url);}});}HBox hbox = new HBox();hbox.getChildren().addAll(hpls);vbox.getChildren().addAll(hbox, browser);VBox.setVgrow(browser, Priority.ALWAYS);stage.setScene(scene);stage.show();} }

超鏈接是在for類似于例18-2中的循環內創建的。為超鏈接設置的操作將相應的URL從urls數組傳遞到WebEngine嵌入式瀏覽器的對象。

編譯并運行此應用程序時,它會生成如圖18-4所示的窗口。

圖18-4從Oracle公司站點加載頁面

?

相關的API文檔?

  • Hyperlink

  • Labeled

  • WebView

  • WebEngine

總結

以上是生活随笔為你收集整理的JavaFX UI控件教程(十九)之Hyperlink的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。