Javafx 简明教程

Hyperlinks 是用户界面组件,当单击时允许用户导航到网页或执行操作。它们类似于按钮,但外观和行为不同。例如,我们可以找到显示窗口顶部地址栏中任何网页的超链接,如下图所示 −

Hyperlinks are UI components that allow users to navigate to a web page or perform an action when clicked. They are similar to buttons but have different appearances and behavior. For example, we can find hyperlinks of any web page inside the address bar located at the top of display window as shown in the below figure −

sample hyperlink

在 JavaFX 中,超链接由名为 Hyperlink 的类表示。此类属于 javafx.scene.control 软件包。通过实例化此类,我们可以在 JavaFX 中创建一个超链接。Hyperlink 类的构造函数如下 −

In JavaFX, the hyperlink is represented by a class named Hyperlink. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a hyperlink in JavaFX. Constructors of the Hyperlink class are listed below −

  1. Hyperlink() − It is the default constructor that constructs a hyperlink without label text.

  2. Hyperlink(String str) − It creates a new hyperlink with the specified label text.

  3. Hyperlink(String str, Node icons) − It creates a new hyperlink with the specified text and graphics labels.

在 JavaFX 中,超链接以文本和图片两种形式创建。在创建超链接时,我们的第一步将使用上述任何构造函数来实例化 Hyperlink 类。接下来,指定用户单击链接时应当执行的操作。要执行此操作,我们需要向 setOnAction() 方法添加 EventHandler 。此外,此 EventHandler 将调用帮助导航的指定方法。

In JavaFX, the hyperlinks are created in two forms namely text and image. While creating a hyperlink, our first step would be instantiating the Hyperlink class by using any of the above mentioned constructors. Then, specify the action that should be performed when the user clicks the link. To do so, we need to add an EventHandler to its setOnAction() method. Furthermore, this EventHandler will call the specified method which helps in navigation.

通过更改 setOnAction() 方法的实现,我们可以为本地资源和远程服务器上可用的资源创建超链接。

By changing the implementation of setOnAction() method, we can create hyperlinks for both local resources as well as those resources that are available on a remote server.

Example

以下 JavaFX 程序中,我们将文本创建为超链接。将此代码保存在名 _ HyperlinkExample.java _ 的文件中。

In the following JavaFX program, we will create a hyperlink as a text. Save this code in a file with the name 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://www.tutorialspoint.com/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 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

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

执行时,以上程序将生成以下输出。

When we execute, the above program will generate the following output.

hyperlink output

要创建带有图片的超链接,请实例化 ImageView 类,并将其对象作为 Hyperlink 类构造函数的参数值传递,如下一个示例所示。将此代码保存在名为 HyperlinkImage.java 的文件中。

To create hyperlink with an image, instantiate the ImageView class and pass its obejct as a parameter value to the constructor of Hyperlink class as shown in the next example. Save this code in a file with the name 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://www.tutorialspoint.com/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 文件 −

Compile and execute the saved Java file from the command prompt using the following commands −

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

Output

在执行上述代码时,它将生成以下输出。

On executing the above code, it will generate the following output.

hyperlink output2