如何在 JavaFX 中設定影像為超連結?


超連結是一個響應單擊和滾動的 UI 元件。你可以透過例項化 javafx.scene.control.Hiperlink 類來建立一個超連結。可以使用 setGraphic() 方法將影像設定為超連結。

示例

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.geometry.Insets;
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.paint.Color;
import javafx.stage.Stage;
public class HyperLinkSettingGraphic extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a hyper link
      Hyperlink link = new Hyperlink();
      //Creating a graphic
      ImageView view = new ImageView();
      InputStream stream = new FileInputStream("D:\images\logo.jpg");
      Image image = new Image(stream);
      view.setImage(image);
      view.setFitHeight(100);
      view.setFitWidth(200);
      //Setting the graphic to the hyperlink
      link.setGraphic(view);
      //Creating a vbox to hold the pagination
      VBox vbox = new VBox();
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(50, 50, 50, 170));
      vbox.getChildren().addAll(link);
      //Setting the stage
      Group root = new Group(vbox);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("Hyperlink");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 19-5-2020

505 次瀏覽

啟動您的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.