📅  最后修改于: 2020-10-14 06:03:11             🧑  作者: Mango
在JavaFx中,我们可以使用超链接来引用网页。它类似于HTML中的锚链接。 javafx.scene.control.HyperLink类提供了处理JavaFX超链接的所有必要方法。
以下代码将HyperLink实现到我们的应用程序中。
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HyperLinkTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Hyperlink hp = new Hyperlink("http://www.javatpoint.com");
StackPane root = new StackPane();
hp.setOnAction(e -> System.out.println("Link Clicked"));
root.getChildren().add(hp);
Scene scene=new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Hyperlink Example");
primaryStage.show();
}
}
输出:
我们可以通过调用实例方法setGraphic()将图像附加到超链接。它接受ImageView类的对象。以下代码将图像附加超链接。
package application;
import java.io.FileInputStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HyperLinkTest extends Application {
publicstaticvoid main(String[] args) {
launch(args);
}
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Hyperlink hp = new Hyperlink();
hp.setOnAction(e -> System.out.println("link clicked"));
FileInputStream input = new FileInputStream("/home/javatpoint/Desktop/JavaFX/Images/hyperlink.png");
Image img = new Image(input);
ImageView imgview=new ImageView(img);
hp.setGraphic(imgview);
StackPane root = newStackPane();
root.getChildren().add(hp);
Scene scene = new Scene(root,300,400);
primaryStage.setScene(scene);
primaryStage.setTitle("Hyperlink Example");
primaryStage.show();
}
}
输出: