Hyperlink的本身效果类似于一个html的网址,不是类似简直就是一模一样,但是点击不会跳转,需要新增一个点击事件才会像是网页那样跳转。代码以及效果如下所示:
package sample;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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 Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox box = new VBox();
Hyperlink link = new Hyperlink("www.baidu.com",new Button("点击跳转网址"));//类似于html的一个效果但是不跳转。如果要跳转需要新增点击事件
link.setOnAction(new EventHandler<ActionEvent>() {//可以跳转的操作
@Override
public void handle(ActionEvent event) {
HostServices hostServices = Main.this.getHostServices();
hostServices.showDocument(link.getText());
}
});
box.getChildren().add(link);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.setTitle("Java FX - Hyperlink ");
primaryStage.setWidth(500);
primaryStage.setHeight(300);
primaryStage.setResizable(false);
primaryStage.show();
}
}