JavaFX2.0 超链接Hyperlink.doc_第1页
JavaFX2.0 超链接Hyperlink.doc_第2页
JavaFX2.0 超链接Hyperlink.doc_第3页
JavaFX2.0 超链接Hyperlink.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Hyperlink类呈现的是Labeled控件的另一种形式,主要用来格式化超链接文本。Figure 17-1显示了默认超链接的三个实现状态。Figure 17-1 Three States of a Hyperlink ControlDescription of Figure 17-1 Three States of a Hyperlink Control创建Hyperlink这些代码将产生上面的效果Example 17-1.Example 17-1 Typical HyperlinkHyperlink link = new Hyperlink(); link.setText(); link.setOnAction(new EventHandler() Override public void handle(ActionEvent e) System.out.println(This link is clicked); );setText实例方法定义了超链接的文本。由于Hyperlink类继承了Labeled类,所以可以为超链接指定特定的字体和内容。setOnAction方法定义了任何时候点击超链接的行为,和Button控件很像。在Example 17-1中,这种行为只是用来打印一个字符串。实际上,它可以实现更多更复杂的认为。连接到本地内容Figure 17-2中的应用显示了本地的图片。Figure 17-2 Viewing ImagesDescription of Figure 17-2 Viewing Images看下它的代码Example 17-2.Example 17-2 Using Hyperlinks to VIew Imagesimport 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 Hyperlinkcaptions.length; final Image images = new ImageimageFiles.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 = hplsi = new Hyperlink(captionsi); final Image image = imagesi = new Image( getClass().getResourceAsStream(imageFilesi) ); hpl.setOnAction(new EventHandler() Override public void handle(ActionEvent e) selectedImage.setImage(image); ); final Button button = new Button(Refresh links); button.setOnAction(new EventHandler() Override public void handle(ActionEvent e) for (int i = 0; i captions.length; i+) hplsi.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 HyperlInksfinal Button button = new Button(Refresh links); button.setOnAction(new EventHandler() Override public void handle(ActionEvent e) for (int i = 0; i captions.length; i+) hplsi.setVisited(false); selectedImage.setImage(null); );点击Refresh Links按钮就会就把超链接都充值为未访问状态。见Figure 17-3.Figure 17-3 Unvisited HyperlinksDescription of Figure 17-3 Unvisited Hyperlinks由于Hyperlink类继承了Labeled类,所以除了文本还可以为其指定图片。下一部分的应用就使用了文本和图片链接并加载远程HTML页面。链接到远程内容可以在JavaFX应用中显示HTML内容,方法是在场景内绑定WebView浏览器。WebView组件提供了网页的基本浏览功能。除此之外,还支持用户交互,如导航和执行JavaScript命令。研究Example 17-4中的代码,它创建了带有文本和图像的超链接。点击超链接后,相应的值就作为URL传递给绑定的浏览器。Example 17-4 Loading Remote Web Pagesimport 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 /us/products/index.html, /, /partners/index.html, /us/support/index.html ; final ImageView selectedImage = new ImageView(); final Hyperlink hpls = new Hyperlinkcaptions.length; final Image images = new ImageimageFiles.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 = hplsi = new Hyperlink(captionsi); final Image image = imagesi = new Image(getClass().getResourceAsStream(imageFilesi); hpl.setGraphic(new ImageView (image); hpl.setFont(Font.font(Arial, 14); final String url = urlsi; hpl.setOnAction(new EventHandler() Override public void handle(ActionEvent e) webEngine.load(url); ); HBox hbox = new HBox(); hbox.getChildren().addAll(hpls); vbox.getChildren().addAll(hbox, browser); VBox.setVgrow(brows

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论