openplanning

Hướng dẫn và ví dụ JavaFX Hyperlink

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- JavaFX Hyperlink

JavaFX Hyperlink mô tả một siêu liên kết, nó tương tự với siêu liên kết trong HTML.

Hyperlink hyperlink = new Hyperlink("Go to Eclipse home page");

hyperlink.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        getHostServices().showDocument("https://eclipse.org");
    }
});
Theo mặc định JavaFX Hyperlink có 3 trạng thái được minh họa như hình dưới đây. Chú ý rằng bạn có thể sử dụng CSS để thay đổi chúng.

2- Ví dụ với Hyperlink

Ví dụ dưới đây tạo một Hyperlink mà khi bạn click vào nó, nó chuyển tới trang chủ của Eclipse (https://eclipse.org).
HyperlinkDemo.java

package org.o7planning.javafx.hyperlink;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class HyperlinkDemo extends Application {

    @Override
    public void start(Stage stage) {

        Hyperlink hyperlink = new Hyperlink("Go to Eclipse home page");

        hyperlink.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                getHostServices().showDocument("https://eclipse.org");
            }
        });

        FlowPane root = new FlowPane();
        root.setPadding(new Insets(10));
        root.getChildren().addAll(hyperlink);
        Scene scene = new Scene(root);

        stage.setTitle("JavaFX Hiperlink (o7planning.org)");

        stage.setWidth(400);
        stage.setHeight(200);

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

    public static void main(String[] args) {
        launch(args);
    }

}

3- Tùy biến Hyperlink

Với Hyperlink bạn có một vài phương thức hữu ích:

public final void setUnderline(boolean value)

public final boolean isUnderline()

public final void setVisited(boolean value)

public final boolean isVisited()
Ví dụ:
HyperlinkDemo2.java

package org.o7planning.javafx.hyperlink;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HyperlinkDemo2 extends Application {

    private final String TEXT = "Go to Eclipse home page";
   
    @Override
    public void start(Stage stage) {

        Hyperlink hyperlink = new Hyperlink(TEXT);

        hyperlink.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                getHostServices().showDocument("https://eclipse.org");
            }
        });

        Button button1 = new Button("On/Off Visited");

        button1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                boolean visited = hyperlink.isVisited();
                hyperlink.setVisited(!visited);
                hyperlink.setText(TEXT+" (visited:"+ hyperlink.isVisible()+")");
            }
        });
       
        Button button2 = new Button("On/Off Underline");

        button2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                boolean underline = hyperlink.isUnderline();
                hyperlink.setUnderline(!underline);
                hyperlink.setText(TEXT+" (underline:"+ hyperlink.isUnderline()+")");
            }
        });
         

        VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setSpacing(10);
        root.getChildren().addAll(hyperlink,button1,button2);
        Scene scene = new Scene(root);

        stage.setTitle("JavaFX Hiperlink (o7planning.org)");

        stage.setWidth(400);
        stage.setHeight(200);

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

    public static void main(String[] args) {
        launch(args);
    }

}

Xem thêm các chuyên mục: