(翻译)第二十三回 JavaFX2.0 超链接Hyperlink

小编 2026-06-07 阅读:1755 评论:0
原文地址http://download.oracle.com/javafx/2.0/ui...

原文地址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

(翻译)第二十三回 JavaFX2.0 超链接Hyperlink
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("http://example.com");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 中的应用显示了本地的图片。

 

 

看下它的代码 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

(翻译)第二十三回 JavaFX2.0 超链接Hyperlink
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[]{        "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

(翻译)第二十三回 JavaFX2.0 超链接Hyperlink
Description of "Figure 17-4 Loading Pages from the Oracle Corporate Site"

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表