超鏈接類表示類似于JavaFX的網(wǎng)頁上的錨鏈接的超鏈接。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Hyperlink; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); Hyperlink link = new Hyperlink("www.15014759268.cn"); root.getChildren().addAll(link); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
以下代碼使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建超鏈接對象。然后它設(shè)置一個(gè)URL作為文本標(biāo)題,最后添加點(diǎn)擊事件處理程序。
Hyperlink link = new Hyperlink(); link.setText("http://www.15014759268.cn"); link.setOnAction((ActionEvent e) -> { System.out.println("This link is clicked"); });
setText實(shí)例方法定義超鏈接的文本標(biāo)題。
超鏈接類擴(kuò)展了Labeled類,我們可以為超鏈接設(shè)置字體和填充。
以下代碼將圖像添加到超鏈接控件。
Hyperlink hpl = new Hyperlink("www.15014759268.cn"); Image image1 = new Image(new File("a.jpg").toURI().toString(), 0, 100, false, false); hpl.setGraphic(new ImageView (image1));
更改超鏈接的字體
import java.io.File; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Hyperlink; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); Hyperlink hpl = new Hyperlink("w3cschool.cn"); hpl.setFont(Font.font("Arial", 14)); root.getChildren().addAll(hpl); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
更多建議: