本章我们来介绍,如何把文本转化成超链接。
Hyperlink类代表了另一种形式的Label。下图描述了3中状态的超链接:
下面的代码块创建了一个超链接
Hyperlink link = new Hyperlink(); link.setText("http://example.com"); link.setOnAction((ActionEvent e) -> { System.out.println("This link is clicked"); });
实例方法setText定义了超链接的文本内容。因为Hyperlink类是Labeled类的扩展,所以我们可以指定内容和字体。setOnAction方法设置了一个特定的事件,当超链接被点击的时候这个事件就触发了,这和Button是一样的。上面的例子当点击的时候打印一行字符串,当然在你的例子中你可能会做更复杂的操作。
源码如下:
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class HyperlinkSample extends Application { final static String[] imageFiles = new String[]{ "product.png", "education.png", "partners.png", "support.png" }; final static String[] captions = new String[]{ "Products", "Education", "Partners", "Support" }; final ImageView selectedImage = new ImageView(); final ScrollPane list = new ScrollPane(); final Hyperlink[] hpls = new Hyperlink[captions.length]; final Image[] images = new Image[imageFiles.length]; public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Hyperlink Sample"); stage.setWidth(300); stage.setHeight(200); selectedImage.setLayoutX(100); selectedImage.setLayoutY(10); for (int i = 0; i < captions.length; i++) { final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]); final Image image = images[i] = new Image( getClass().getResourceAsStream(imageFiles[i]) ); hpl.setOnAction((ActionEvent e) -> { selectedImage.setImage(image); }); } final Button button = new Button("Refresh links"); button.setOnAction((ActionEvent e) -> { for (int i = 0; i < captions.length; i++) { hpls[i].setVisited(false); selectedImage.setImage(null); } }); VBox vbox = new VBox(); vbox.getChildren().addAll(hpls); vbox.getChildren().add(button); vbox.setSpacing(5); ((Group) scene.getRoot()).getChildren().addAll(vbox, selectedImage); stage.setScene(scene); stage.show(); } }
如果我们在应用中使用WebView浏览器,我们就可以加载HTML的内容。WebView提供了基本的网页浏览能力。它可以加载web网页并且支持交互,比如:导航链接和执行javaScript命令。
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Pos; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class HyperlinkWebViewSample extends Application { final static String[] imageFiles = new String[]{ "product.png", "education.png", "partners.png", "support.png" }; final static String[] captions = new String[]{ "Products", "Education", "Partners", "Support" }; final static String[] urls = new String[]{ "http://www.oracle.com/us/products/index.html", "http://education.oracle.com/", "http://www.oracle.com/partners/index.html", "http://www.oracle.com/us/support/index.html" }; final ImageView selectedImage = new ImageView(); final Hyperlink[] hpls = new Hyperlink[captions.length]; final Image[] images = new Image[imageFiles.length]; public static void main(String[] args){ launch(args); } @Override public void start(Stage stage) { VBox vbox = new VBox(); Scene scene = new Scene(vbox); stage.setTitle("Hyperlink Sample"); stage.setWidth(570); stage.setHeight(550); selectedImage.setLayoutX(100); selectedImage.setLayoutY(10); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); for (int i = 0; i < captions.length; i++) { final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]); final Image image = images[i] = new Image(getClass().getResourceAsStream(imageFiles[i])); hpl.setGraphic(new ImageView (image)); hpl.setFont(Font.font("Arial", 14)); final String url = urls[i]; hpl.setOnAction((ActionEvent e) -> { webEngine.load(url); }); } HBox hbox = new HBox(); hbox.setAlignment(Pos.BASELINE_CENTER); hbox.getChildren().addAll(hpls); vbox.getChildren().addAll(hbox, browser); VBox.setVgrow(browser, Priority.ALWAYS); stage.setScene(scene); stage.show(); } }
运行如下图所示: