这一章讲述用来将文本转换为超链接的 Hyperlink
组件
Hyperlink
类 是 Labeled 类的另一种形式。
图18-1 展示了默认超链接实现的3中状态
图 18-1 超链接组件的3中状态

创建一个超链接
例 18-1 中展示创建超链接的代码片段
1. <em>例18-1 典型的超链接
2. </em>Hyperlink link = new Hyperlink();
3. link.setText("http://example.com");
4. link.setOnAction(new EventHandler<ActionEvent>() {
5. @Override
6. public void handle(ActionEvent e) {
7. System.out.println("This link is clicked");
8. }
9. });
setText
成员方法用来定义 超链接的标题。
因为 Hyperlink 类是Labeled类的一个拓展,你可以为标题设置特定的字体和文字。
setOnAction
方法用来指定超链接点击时的行为。类似于Button的 onAction动作。
例 18-1, 中行为仅限于打印字符串。但是在你的应用里面,你可能想用来实现更常见的任务。
链接本地内容
在图18-2展示应用中从本地目录中渲染图片
图 18-2 显示图片

展示例 18-2的源代码:
例 18-2利用超链接浏览图片
1. import javafx.application.Application;
2. import javafx.event.ActionEvent;
3. import javafx.event.EventHandler;
4. import javafx.scene.*;
5. import javafx.scene.control.*;
6. import javafx.scene.image.Image;
7. import javafx.scene.image.ImageView;
8. import javafx.scene.layout.VBox;
9. import javafx.stage.Stage;
10.
11. public class Main extends Application {
12.
13. final static String[] imageFiles = new String[]{
14. "product.png",
15. "education.png",
16. "partners.png",
17. "support.png"
18. };
19. final static String[] captions = new String[]{
20. "Products",
21. "Education",
22. "Partners",
23. "Support"
24. };
25. final ImageView selectedImage = new ImageView();
26. final ScrollPane list = new ScrollPane();
27. final Hyperlink[] hpls = new Hyperlink[captions.length];
28. final Image[] images = new Image[imageFiles.length];
29.
30. public static void main(String[] args) {
31. Application.launch(args);
32. }
33.
34. @Override
35. public void start(Stage stage) {
36. Scene scene = new Scene(new Group());
37. stage.setTitle("Hyperlink Sample");
38. stage.setWidth(300);
39. stage.setHeight(200);
40.
41. selectedImage.setLayoutX(100);
42. selectedImage.setLayoutY(10);
43.
44. for (int i = 0; i < captions.length; i++) {
45. final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
46. final Image image = images[i] = new Image(
47. getClass().getResourceAsStream(imageFiles[i])
48. );
49. hpl.setOnAction(new EventHandler<ActionEvent>() {
50. @Override
51. public void handle(ActionEvent e) {
52. selectedImage.setImage(image);
53. }
54. });
55. }
56.
57. final Button button = new Button("Refresh links");
58. button.setOnAction(new EventHandler<ActionEvent>() {
59. @Override
60. public void handle(ActionEvent e) {
61. for (int i = 0; i < captions.length; i++) {
62. hpls[i].setVisited(false);
63. selectedImage.setImage(null);
64. }
65. }
66. });
67.
68. VBox vbox = new VBox();
69. vbox.getChildren().addAll(hpls);
70. vbox.getChildren().add(button);
71. vbox.setSpacing(5);
72.
73. ((Group) scene.getRoot()).getChildren().addAll(vbox, selectedImage);
74. stage.setScene(scene);
75. stage.show();
76. }
77. }<em>
78. </em>
程序通过for循环创建4个Hyperlink
,用户点击某个链接是将触发此超链接setOnAction
方法里定义的行为的动作。
因此图片数组中的对应的图片设置到selectedImage
变量中。当用户点击一个超链接,超链接将显示被访问过。你可以通过调用setVisited
方法来刷新超链接。
例18-3 的代码片段即实现了该任务。
例18-3 刷新超链接
1. final Button button = new Button("Refresh links");
2. button.setOnAction(new EventHandler<ActionEvent>() {
3. @Override
4. public void handle(ActionEvent e) {
5. for (int i = 0; i < captions.length; i++) {
6. hpls[i].setVisited(false);
7. selectedImage.setImage(null);
8. }
9. }
10. });
正如图18-3所示,当点击刷新按钮后,所有的的超链接都被设置为未访问状态。
图18-3 未访问过的超链接
因为Hyperlink
类拓展自Labeled
类,你不仅可以指定标题的文字还为其设置一张图片。下一节的程序将展示既使用标题也使用图片来创建超链接和加载远程html页面。
链接远程内容
在你的JavaFx程序中,通过嵌入WebView
浏览器组件来渲染 HTML内容。WebView
组件提供浏览网页的基本功能。该组件可以渲染网页支持用户和链接的交互也可以执行JavaScript代码。
学习例18-4的源码。它创建了4个带标题和图片的超链接。当点击其中一个超链接时,对应的值作为URL传给镶嵌的浏览器。
例 18-4 加载远程网页
1. import javafx.application.Application;
2. import javafx.event.ActionEvent;
3. import javafx.event.EventHandler;
4. import javafx.scene.*;
5. import javafx.scene.control.*;
6. import javafx.scene.image.Image;
7. import javafx.scene.image.ImageView;
8. import javafx.scene.layout.HBox;
9. import javafx.scene.layout.Priority;
10. import javafx.scene.layout.VBox;
11. import javafx.scene.text.Font;
12. import javafx.scene.web.WebEngine;
13. import javafx.scene.web.WebView;
14. import javafx.stage.Stage;
15.
16. public class Main extends Application {
17.
18. final static String[] imageFiles = new String[]{
19. "product.png",
20. "education.png",
21. "partners.png",
22. "support.png"
23. };
24. final static String[] captions = new String[]{
25. "Products",
26. "Education",
27. "Partners",
28. "Support"
29. };
30.
31. final static String[] urls = new String[]{
32. "http://www.oracle.com/us/products/index.html",
33. "http://education.oracle.com/",
34. "http://www.oracle.com/partners/index.html",
35. "http://www.oracle.com/us/support/index.html"
36. };
37.
38. final ImageView selectedImage = new ImageView();
39. final Hyperlink[] hpls = new Hyperlink[captions.length];
40. final Image[] images = new Image[imageFiles.length];
41.
42. public static void main(String[] args){
43. launch(args);
44. }
45.
46. @Override
47. public void start(Stage stage) {
48. VBox vbox = new VBox();
49. Scene scene = new Scene(vbox);
50. stage.setTitle("Hyperlink Sample");
51. stage.setWidth(570);
52. stage.setHeight(550);
53.
54. selectedImage.setLayoutX(100);
55. selectedImage.setLayoutY(10);
56.
57. final WebView browser = new WebView();
58. final WebEngine webEngine = browser.getEngine();
59.
60. for (int i = 0; i < captions.length; i++) {
61. final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
62.
63. final Image image = images[i] =
64. new Image(getClass().getResourceAsStream(imageFiles[i]));
65. hpl.setGraphic(new ImageView (image));
66. hpl.setFont(Font.font("Arial", 14));
67. final String url = urls[i];
68.
69. hpl.setOnAction(new EventHandler<ActionEvent>() {
70. @Override
71. public void handle(ActionEvent e) {
72. webEngine.load(url);
73. }
74. });
75. }
76.
77. HBox hbox = new HBox();
78. hbox.getChildren().addAll(hpls);
79.
80. vbox.getChildren().addAll(hbox, browser);
81. VBox.setVgrow(browser, Priority.ALWAYS);
82.
83. stage.setScene(scene);
84. stage.show();
85. }
86. }
类似例18-2超链接通过for循环进行创建。为超链接设置行为传递给urls数组对应的URL地址给嵌套在浏览器WebEngine
对象。
当编译运行此程序,程序窗体将显示如图18-4的状况。
图18-4 从Oracle 公司网址加载页面
