收藏 分销(赏)

JavaFX2.0 超链接Hyperlink.doc

上传人:pc****0 文档编号:6559555 上传时间:2024-12-13 格式:DOC 页数:4 大小:199.50KB 下载积分:10 金币
下载 相关 举报
JavaFX2.0 超链接Hyperlink.doc_第1页
第1页 / 共4页
JavaFX2.0 超链接Hyperlink.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
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 Hyperlink(); link.setText(""); link.setOnAction(new EventHandler<ActionEvent>() { @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 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; 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 . Figure 17-3 Unvisited Hyperlinks Description of "Figure 17-3 Unvisited Hyperlinks"  由于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[]{ " " " " }; 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"
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服