JavaFX - 超連結



超連結是允許使用者在點選時導航到網頁或執行操作的 UI 元件。它們類似於按鈕,但外觀和行為不同。例如,我們可以在顯示視窗頂部的位址列中找到任何網頁的超連結,如下面的圖所示:

Hyperlinks

在 JavaFX 中建立超連結

在 JavaFX 中,超連結由名為Hyperlink的類表示。此類屬於包javafx.scene.control。透過例項化此類,我們可以在 JavaFX 中建立超連結。Hyperlink 類的建構函式如下所示:

  • Hyperlink() - 這是預設建構函式,它建立一個沒有標籤文字的超連結。

  • Hyperlink(String str) - 它建立一個具有指定標籤文字的新超連結。

  • Hyperlink(String str, Node icons) - 它建立一個具有指定文字和圖形標籤的新超連結。

在 JavaFX 中,超連結以文字和影像兩種形式建立。建立超連結時,我們的第一步是使用上述任何建構函式例項化 Hyperlink 類。然後,指定使用者點選連結時應執行的操作。為此,我們需要向其setOnAction()方法新增一個EventHandler。此外,此 EventHandler 將呼叫指定的方法,這有助於導航。

透過更改setOnAction()方法的實現,我們可以為本地資源以及遠端伺服器上可用的資源建立超連結。

示例

在下面的 JavaFX 程式中,我們將建立一個文字超連結。將此程式碼儲存在名為HyperlinkExample.java的檔案中。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class HyperlinkExample extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label labelText = new Label("On clicking the below text, it will redirect us to tutorialspoint");
      // Create a hyperlink with text
      Hyperlink textLink = new Hyperlink("Visit TutorialsPoint");
      // Set the action of the hyperlink
      textLink.setOnAction(new EventHandler() {
         @Override
         public void handle(ActionEvent event) {
            // Open the web page in the default browser
            getHostServices().showDocument("https://tutorialspoint.tw/index.htm");
         }
      });
      // Create a scene with the hyperlink
      VBox root = new VBox(labelText, textLink);
      root.setAlignment(Pos.CENTER); 
      Scene scene = new Scene(root, 400, 300);
      // Set the title and scene of the stage
      stage.setTitle("Hyperlink in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要從命令提示符編譯和執行儲存的 Java 檔案,請使用以下命令:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkExample

輸出

執行上述程式將生成以下輸出。

Hyperlink Output

建立影像超連結

要建立帶有影像的超連結,請例項化ImageView類並將它的物件作為引數值傳遞給Hyperlink類的建構函式,如下一個示例所示。將此程式碼儲存在名為HyperlinkImage.java的檔案中。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class HyperlinkImage extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label labelText = new Label("On clicking the below image, it will redirect us to tutorialspoint");
      // Create a hyperlink with image
      Image image = new Image("tutorials_point.jpg");
      ImageView imageV = new ImageView(image);
      imageV.setFitWidth(150);
      imageV.setFitHeight(150);
      Hyperlink imageLink = new Hyperlink("visit: ", imageV);
      // Set the content display position of the image
      imageLink.setContentDisplay(ContentDisplay.RIGHT);
      // Set the action of the hyperlink
      imageLink.setOnAction(new EventHandler() {
         @Override
         public void handle(ActionEvent event) {
            // Open the web page in the default browser
            getHostServices().showDocument("https://tutorialspoint.tw/index.htm");
         }
      });
      // Create a scene with the hyperlink
      VBox root = new VBox(labelText, imageLink);
      root.setAlignment(Pos.CENTER); 
      Scene scene = new Scene(root, 400, 300);
      // Set the title and scene of the stage
      stage.setTitle("Hyperlink in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

使用以下命令從命令提示符編譯並執行儲存的 Java 檔案:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkImage.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HyperlinkImage

輸出

執行上述程式碼將生成以下輸出。

Hyperlink Image
廣告