JavaFX:设置控件的Tooltip

package javafx8.ch06;

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @copyright 2023-2022
 * @package   javafx8.ch06
 * @file      TooltipAnchoringDemo.java
 * @date      2023-08-24 13:32
 * @author    qiao wei
 * @version   1.0
 * @brief     
 * @history
 */
public class TooltipAnchoringDemo extends Application {
    
    public TooltipAnchoringDemo() {}
    
    @Override
    public void start(Stage stage) {
        // Initialize tooltip controls
        Label label1 = new Label("TEST1\nTEST1\nTEST1");
        label1.setStyle("-fx-background-color: red;");
        Label label2 = new Label("TEST2\nTEST2\nTEST2");
        label2.setStyle("-fx-background-color: green;");
        Label label3 = new Label("TEST3\nTEST3\nTEST3");
        label3.setStyle("-fx-background-color: blue;");
        
        // Associates the given Tooltip with the given Node.
        CustomTooltip tooltip = CustomTooltip.install("TOOLTIP 1", label1);
        tooltip.setShowDelay(Duration.seconds(0.01));
        CustomTooltip.install("TOOLTIP 2", label2);
        CustomTooltip.install("TOOLTIP 3", label3);
        
        // Set 15px is the amount of horizontal space between each child controls.
        HBox root = new HBox(15, label1, label2, label3);
        
        // 设置容器内控件距离容器边框距离。
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.BOTTOM_RIGHT);

        Scene scene = new Scene(root, 300, 180);
        stage.setScene(scene);
        stage.setTitle("Tooltip Demo");

        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(TooltipAnchoringDemo.class, args);
    }
}

/**
 * @copyright 2023-2022
 * @package   javafx8.ch06
 * @file      TooltipAnchoringDemo.java
 * @date      2023-08-24 14:38
 * @author    qiao wei
 * @version   1.0
 * @brief     Custom tooltip
 * @history
 */
class CustomTooltip extends Tooltip {
    
    public CustomTooltip(final String s) {
        super(s);
    }
    
    /**
     * @class   CustomTooltip
     * @date    2023-08-24 14:09
     * @author  qiao wei
     * @version 1.0
     * @brief   Binds control and tooltip
     * @param   message tooltip's message
     * @param   node Binded control
     * @return  
     * @throws
     */
    public static CustomTooltip install(String message, Node node) {
        CustomTooltip tooltip = new CustomTooltip(message);
        tooltip.node = node;
        install(node, tooltip);
        
        return tooltip;
    }
    
    @Override
    protected void show() {
        // Set the new position before showing.
        Point2D anchor = anchor(node);
        setAnchorX(anchor.getX());
        setAnchorY(anchor.getY());

        super.show();
    }
    
    /**
     * @class   CustomTooltip
     * @date    2023-08-24 13:42
     * @author  qiao wei
     * @version 1.0
     * @brief   Calculates the anchor point for the tooltip.
     * @param   node The owner of tooltip
     * @return  The anchor position.
     * @throws
     */
    private Point2D anchor(final Node node) {
        // Get the node bounds on the screen.
        final Bounds bounds = node.localToScreen(node.getBoundsInLocal());
        
        // Calculate the opening position based on node bounds for screen bounds.
        final Point2D openPosition = new Point2D(bounds.getMaxX(), bounds.getMaxY() + Y_OFFSET);
        
        return openPosition;
    }
    
    private Node node;
    
    /**
     * Pixel offset from the node in the y-axis to display the tooltip.
     * You can keep it to 0. Normally I don't want to keep it very near to the node.
     */
    private static final int Y_OFFSET = 2;
}

猜你喜欢

转载自blog.csdn.net/weiweiqiao/article/details/133433902