1、Hyperlink 类呈现的是Labeled 控件的另一种形式,主要用来格式化超链接文本。Figure 17-1 显示了默认超链接的三个实现状态。 Figure 17-1 Three States of a Hyperlink Control Description of "Figure 17-1 Three States of a Hyperlink Control" 创建Hyperlink 这些代码将产生上面的效果 Example 17-1 . Example 17-1 Typical Hyperlink Hyperlink link = new Hyperl
2、ink(); link.setText(""); link.setOnAction(new EventHandler
3、种行为只是用来打印一个字符串。实际上,它可以实现更多更复杂的认为。 连接到本地内容 Figure 17-2 中的应用显示了本地的图片。 Figure 17-2 Viewing Images Description of "Figure 17-2 Viewing Images" 看下它的代码 Example 17-2 . Example 17-2 Using Hyperlinks to VIew Images import javafx.application.Application; import javafx.event.ActionEvent; impor
4、t javafx.event.EventHandler; 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 Main extends Application { final static String[] imageFiles = new String[]
5、{ "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[captio
6、ns.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); select
7、edImage.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(new EventHandler
8、 @Override public void handle(ActionEvent e) { selectedImage.setImage(image); } }); } final Button button = new Button("Refresh links"); button.setOnAction(new EventHandler
9、alse); 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(); } } 该应用在for循环中创建了四个 Hyperlink 对象。点击特点的超
10、链接会调用setOnAction 方法产生不同的行为。这样,images数组中相应的图片就设置给 selectedImage 变量。
当点击一个超链接时,它就成为了访问过的(visited)。可以使用Hyperlink 类的setVisited 方法刷新链接。见Example 17-3 中的代码。
Example 17-3 Refreshing the HyperlInks
final Button button = new Button("Refresh links"); button.setOnAction(new EventHandler
11、Override public void handle(ActionEvent e) { for (int i = 0; i < captions.length; i++) { hpls[i].setVisited(false); selectedImage.setImage(null); } } }); 点击Refresh Links按钮就会就把超链接都充值为未访问状态。见Figure 17-3 . Figure 17-3 Unvisited Hyperlinks Description of "Figure 17-3 Unvisited Hyperlinks" 由于Hy
12、perlink类继承了 Labeled 类,所以除了文本还可以为其指定图片。下一部分的应用就使用了文本和图片链接并加载远程HTML页面。 链接到远程内容 可以在JavaFX应用中显示HTML内容,方法是在场景内绑定WebView 浏览器。WebView 组件提供了网页的基本浏览功能。除此之外,还支持用户交互,如导航和执行JavaScript命令。 研究Example 17-4 中的代码,它创建了带有文本和图像的超链接。点击超链接后,相应的值就作为URL传递给绑定的浏览器。 Example 17-4 Loading Remote Web Pages import javafx.a
13、pplication.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; 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.s
14、cene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { final static String[] imageFiles = new String[]{ "product.png", "education.png", "partners.png", "support.png" }; fi
15、nal static String[] captions = new String[]{ "Products", "Education", "Partners", "Support" }; final static String[] urls = new String[]{ " " " " }; final ImageView selectedImage = new ImageView(); final Hyperlink[] hpls = new Hyperlink[captions.length]; final Image[] images = new Image[imageFiles.l
16、ength]; 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)
17、 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(n
18、ew ImageView (image)); hpl.setFont(Font.font("Arial", 14)); final String url = urls[i]; hpl.setOnAction(new EventHandler
19、 browser); VBox.setVgrow(browser, Priority.ALWAYS); stage.setScene(scene); stage.show(); } } 超链接也在for循环中创建,类似于 Example 17-2 。为超链接设置的行为从urls数组到 WebEngine 对象传递了相应的URL。 编译运行效果如Figure 17-4 . Figure 17-4 Loading Pages from the Oracle Corporate Site Description of "Figure 17-4 Loading Pages from the Oracle Corporate Site"






