(通译)第二十三回 JavaFX2.0 超链接Hyperlink
原文地址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"
创建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 中的应用显示了本地的图片。
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[]{
"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"