个性化阅读
专注于IT技术分析

JavaFX超链接

在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.srcmini02.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();
}
}

输出:

JavaFX超链接

用链接附加图像

我们可以通过调用实例方法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/srcmini/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();
}
}

输出:

JavaFX超链接1
赞(0)
未经允许不得转载:srcmini » JavaFX超链接

相关推荐

评论 抢沙发

评论前必须登录!