JavaFX - 工具提示



工具提示是一個小的彈出視窗,當用戶將滑鼠懸停在節點或控制元件上時,它會顯示一些其他資訊。它主要用於解釋按鈕、選單或影像的功能,或為文字欄位提供一些提示。在下圖中,我們可以看到工具提示解釋了選單的功能:

Tooltip

JavaFX 中的工具提示

在 JavaFX 中,工具提示由一個名為Tooltip的類表示,它是javafx.scene.control包的一部分。此包的每個 UI 元件都帶有一個名為setTooltip()的內建方法,用於關聯工具提示。我們可以透過將其傳遞給setText()方法或使用下面列出的建構函式來指定工具提示文字:

  • Tooltip() - 這是預設建構函式,它構建一個沒有任何文字的工具提示。

  • Tooltip(String str) - 它使用預定義文字構建一個新的工具提示。

在 JavaFX 中建立工具提示的步驟

要在 JavaFX 中建立工具提示,請按照以下步驟操作。

步驟 1:建立與工具提示關聯的節點

我們知道工具提示解釋了指定節點的功能。此節點可以是任何 JavaFX 元件,例如選單、影像和文字欄位。在這裡,我們將使用影像作為節點。要在 JavaFX 中建立影像,請例項化ImageView類並將影像的路徑作為引數值傳遞給其建構函式。

//Passing path of an image 
Image image = new Image(new FileInputStream("tutorials_point.jpg"));  
//Setting the image view 
ImageView imageView = new ImageView(image); 

步驟 2:例項化 Tooltip 類

要在 JavaFX 中建立工具提示,請例項化Tooltip類並將工具提示文字作為引數值傳遞給其建構函式,使用以下程式碼:

//Creating tool tip for the given image
Tooltip toolTipTxt = new Tooltip("This is the new logo of Tutorialspoint");

步驟 3:將工具提示與節點關聯

要將工具提示與指定的節點關聯,我們使用install()方法,該方法接受TooltipImageView物件作為引數。

//Setting the tool tip to the image
Tooltip.install(imageView, toolTipTxt); 

步驟 4:啟動應用程式

建立工具提示後,請按照以下步驟正確啟動應用程式:

  • 首先,建立一個VBox,它垂直儲存節點。

  • 接下來,例項化名為Scene的類,並將 VBox 物件作為引數值傳遞給其建構函式以及應用程式螢幕的尺寸。

  • 然後,使用Stage類的setTitle()方法設定舞臺的標題。

  • 現在,使用名為Stage的類的setScene()方法將 Scene 物件新增到舞臺。

  • 使用名為show()的方法顯示場景的內容。

  • 最後,使用launch()方法啟動應用程式。

示例

在以下示例中,我們將建立一個 JavaFX 應用程式中的工具提示。將此程式碼儲存在名為JavafxTooltip.java的檔案中。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import javafx.stage.Stage;
public class JavafxTooltip extends Application {
   @Override
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a label
      Label labeltext = new Label("Hover over Image to see the details....");
      //Passing path of an image 
      Image image = new Image(new FileInputStream("tutorials_point.jpg"));  
      //Setting the image view 
      ImageView imageView = new ImageView(image); 
      //Setting the position of the image 
      imageView.setX(50); 
      imageView.setY(25); 
      //setting the fit height and width of the image view 
      imageView.setFitHeight(350); 
      imageView.setFitWidth(350); 
      //Setting the preserve ratio of the image view 
      imageView.setPreserveRatio(true);  
      //Creating tool tip for the given image
      Tooltip toolTipTxt = new Tooltip("This is the new logo of Tutorialspoint");
      //Setting the tool tip to the image
      Tooltip.install(imageView, toolTipTxt);
      // to display the content vertically 
      VBox box = new VBox(5);
      box.setPadding(new Insets(25, 5 , 5, 50));
      box.getChildren().addAll(labeltext, imageView);
      //Setting the stage
      Scene scene = new Scene(box, 400, 350);
      stage.setTitle("Example of Tooltip in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

要從命令提示符編譯和執行儲存的 Java 檔案,請使用以下命令:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip

輸出

當我們執行上述程式碼時,它將為影像生成一個工具提示,如下面的輸出所示。

Tooltip Output

向工具提示新增圖示

圖示是小的影像,以圖形方式說明應用程式的命令或功能。要向 JavaFX 工具提示新增圖示,我們使用setGraphic()方法,該方法接受ImageView物件並在工具提示文字的左側顯示圖示。

示例

以下是將建立一個帶圖示的工具提示的 JavaFX 程式。將此程式碼儲存在名為JavafxTooltip.java的檔案中。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import javafx.stage.Stage;
public class JavafxTooltip extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      // Creating a label
      Label labeltext = new Label("Hover over this text to see the tooltip....");
      // Instantiating Tooltip class 
      Tooltip toolT = new Tooltip();
      // Passing path of an image 
      Image icon = new Image(new FileInputStream("faviconTP.png")); 
      // adding the icon for tooltip 
      toolT.setGraphic(new ImageView(icon));
      // adding the text
      toolT.setText("This is the new logo of Tutorialspoint");
      // setting the tooltip to the label
      labeltext.setTooltip(toolT);
      //Setting the stage
      Scene scene = new Scene(labeltext, 400, 300);
      stage.setTitle("Example of Tooltip in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
      public static void main(String args[]){
      launch(args);
   }
}

使用以下命令從命令提示符編譯和執行儲存的 Java 檔案:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip

輸出

執行後,上述程式將生成以下輸出:

Tooltip Output2
廣告