1. 程式人生 > >JavaFX UI控制元件教程(十九)之Hyperlink

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>() {
    @Override
    public 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);
    }
 
    @Override
    public 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>() {
                @Override
                public void handle(ActionEvent e) {
                    selectedImage.setImage(image);
                }
            });
        }
 
        final Button button = new Button("Refresh links");
        button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public 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();
    }
}

此應用程式Hyperlinkfor迴圈中建立四個物件。setOnAction呼叫每個超連結的方法定義使用者單擊特定超連結時的行為。在這種情況下,imagesselectedImage變數設定陣列中的相應影象。

當用戶單擊超連結時,它將被訪問。您可以使用類的setVisited方法Hyperlink重新整理連結。例18-3中的程式碼片段完成了這項任務。

示例18-3重新整理HyperlInks

final Button button = new Button("Refresh links");
button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public 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);
    }
 
    @Override
    public 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>() {
                @Override
                public 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文件