原文地址http://download.oracle.com/javafx/2.0/ui_controls/hyperlink.htm
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"
setText實例方法定義了超鏈接的文本。由於
Hyperlink
類繼承了Labeled
類,所以可以爲超鏈接指定特定的字體和內容。setOnAction
方法定義了任何時候點擊超鏈接的行爲,和Button控件很像。在
Example 17-1 中,這種行爲只是用來打印一個字符串。實際上,它可以實現更多更復雜的認爲。
連接到本地內容
Figure 17-2 中的應用顯示了本地的圖片。
看下它的代碼 Example 17-2 .
Example 17-2 Using Hyperlinks to VIew Images
import javafx.application.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.VBox; import javafx.stage.Stage; public class Main 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(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { selectedImage.setImage(image); } }); } final Button button = new Button("Refresh links"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(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(); } }
該應用在for循環中創建了四個 Hyperlink
對象。點擊特點的超鏈接會調用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<ActionEvent>() { @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 .
由於Hyperlink類繼承了
Labeled
類,所以除了文本還可以爲其指定圖片。下一部分的應用就使用了文本和圖片鏈接並加載遠程HTML頁面。
鏈接到遠程內容
可以在JavaFX應用中顯示HTML內容,方法是在場景內綁定WebView
瀏覽器。WebView
組件提供了網頁的基本瀏覽功能。除此之外,還支持用戶交互,如導航和執行JavaScript命令。
研究Example 17-4 中的代碼,它創建了帶有文本和圖像的超鏈接。點擊超鏈接後,相應的值就作爲URL傳遞給綁定的瀏覽器。
Example 17-4 Loading Remote Web Pages
import javafx.application.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.scene.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" }; 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(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { webEngine.load(url); } }); } HBox hbox = new HBox(); hbox.getChildren().addAll(hpls); vbox.getChildren().addAll(hbox, 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"