JavaFX UI控件教程(十九)之Hyperlink

翻译自   Hyperlink

本章介绍Hyperlink用于将文本格式化为超链接的控件。

所述Hyperlink类表示另一种类型的Labeled控制。图18-1演示了默认超链接实现的三种状态。

图18-1超链接控制的三种状态

创建超链接

示例18-1中显示了生成超链接的代码片段。

例18-1典型的超链接

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控件的工作方式。在例18-1中,此操作仅限于打印字符串。但是,在您的应用程序中,您可能希望实现更常见的任务。

链接本地内容

图18-2中的应用程序从本地目录呈现图像。

图18-2查看图像

查看例18-2中显示的此应用程序的源代码。

示例18-2使用超链接查看图像

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();
    }
}

此应用程序Hyperlinkfor循环中创建四个对象。setOnAction调用每个超链接的方法定义用户单击特定超链接时的行为。在这种情况下,imagesselectedImage变量设置数组中的相应图像。

当用户单击超链接时,它将被访问。您可以使用类的setVisited方法Hyperlink刷新链接。例18-3中的代码片段完成了这项任务。

示例18-3刷新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);
       }
    }
});

单击时,“刷新链接”按钮会将所有超链接带到未访问状态,如图18-3所示

图18-3未访问的超链接

由于Hyperlink该类是类的扩展,因此Labeled您不仅可以指定文本标题,还可以指定图像。下一节中提供的应用程序使用文本标题和图像来创建超链接和加载远程HTML页面。

链接远程内容

您可以通过WebView在应用程序场景中嵌入浏览器来在JavaFX应用程序中呈现HTML内容。该WebView组件提供基本的网页浏览功能。它呈现网页并支持用户交互,例如导航链接和执行JavaScript命令。

研究例18-4中应用程序的源代码。它创建了四个带有文本标题和图像的超链接。单击超链接时,相应的值将作为URL传递给嵌入式浏览器。

示例18-4加载远程网页

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类似于例18-2中的循环内创建的。为超链接设置的操作将相应的URL从urls数组传递到WebEngine嵌入式浏览器的对象。

编译并运行此应用程序时,它会生成如图18-4所示的窗口。

图18-4从Oracle公司站点加载页面

相关的API文档 

猜你喜欢

转载自blog.csdn.net/moakun/article/details/83050782
今日推荐