JavaFX Hyperlink

105 篇文章 5 订阅

The JavaFX Hyperlink control is a text that functions as a button, meaning you can configure a Hyperlink to perform some action when the user clicks it. Just like a hyperlink in a web page. The JavaFX Hyperlink control is represented by the class javafx.scene.control.Hyperlink .

Here is a screenshot showing how a JavaFX Hyperlink looks:

 

JavaFX Hyperlink Example

Here is a full JavaFX Hyperlink example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HyperlinkExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Hyperlink link = new Hyperlink("Click Me!");

        VBox vBox = new VBox(link);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

This example is a full JavaFX application that creates a Hyperlink, inserts it into a JavaFX VBox which is then added to a JavaFX Scene. The Scene is then added to a JavaFX Stage which is then made visible.

Create a Hyperlink

In order to use a JavaFX Hyperlink control you must first create a Hyperlink instance. Here is an example of creating a JavaFX Hyperlink instance:

Hyperlink link = new Hyperlink("Click me!");

Set Hyperlink Text

It is possible to change the text of a JavaFX Hyperlink via its setText() method. Here is an example of changing the text of a JavaFX Hyperlink:

Hyperlink link = new Hyperlink("Click me!");

link.setText("New link text");

Set Hyperlink Font

It is possible to change the font of a JavaFX Hyperlink via its setFont() method. You can read more about creating fonts in my JavaFX Fonts tutorial. Here is an example of setting the font of a JavaFX Hyperlink:

Hyperlink link = new Hyperlink("Click Me!");

Font courierNewFontBold36 = Font.font("Courier New", FontWeight.BOLD, 36);

link.setFont(courierNewFontBold36);

Set Hyperlink Action

To respond to clicks on a JavaFX Hyperlink you set an action listener on the Hyperlink instance. Here is an example of setting an action listener on a JavaFX Hyperlink instance:

Hyperlink link = new Hyperlink("Click me!");

link.setOnAction(e -> {
    System.out.println("The Hyperlink was clicked!");
});

url: JavaFX Hyperlink

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值