亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX 如何在節點上居中顯示工具提示?

JavaFX 如何在節點上居中顯示工具提示?

慕碼人8056858 2022-04-28 15:42:05
我目前正在研究一種表單,該表單在將焦點從一個節點更改為另一個節點時進行驗證,并希望在包含錯誤的節點上方顯示一個工具提示(如果存在)。我正在使用工具提示的show(Node ownerNode, double anchorX, double anchorY)方法簽名來指定我想將它附加到哪個節點以及放置它的位置。我試過以下代碼:Tooltip tooltip = new Tooltip("Error"); tooltip.show(node, 0, 0);double tooltipMiddle = tooltip.getWidth() / 2;tooltip.hide(); double nodeMiddle = node.getWidth() / 2; //Gets the node's x and y position within the windowBounds bounds = node.localToScene(node.getBoundsInLocal());  //Tooltip's bottom-right corner is set to the anchor points, so I set x to//the node's x coordinate plus half the width of the node plus half the width//of the tooltip tooltip.show(node,             bounds.getMinX() + nodeMiddle + tooltipMiddle, bounds.getMinY());這讓我非常接近中心,但它仍然關閉。我一直在互聯網上尋求幫助,但我只是沒有找到任何幫助,所以我想我會在這里問。有沒有機會我可以深入了解為什么我無法按照自己的意愿進行工作?
查看完整描述

2 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

我帶來的代碼提供了工具提示的正確定位,但遠非完美。帶來全面的解決方案需要大量工作(如果您愿意,我們可以討論)。我認為你有一個數學問題,并且Tooltip類可能不是最好的選擇。


import javafx.application.Application;

import javafx.geometry.Bounds;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.layout.Region;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;


public class TooltipApp extends Application {


    public static void main(String[] args) {

        launch(args);

    }


    @Override

    public void start(Stage stage) throws Exception {

        TextField textField = new TextField();

        textField.setPrefWidth(100.);


        Button button = new Button("Tooltip");


        HBox hBox = new HBox(textField, button);

        hBox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);


        Label label = new Label("Empty!");

        label.setVisible(false);

        Pane tooltipPane = new Pane(label);

        tooltipPane.setMouseTransparent(true);


        StackPane stackPane = new StackPane(hBox, tooltipPane);

        stackPane.setPrefSize(600., 400.);


        Scene scene = new Scene(stackPane);


        stage.setScene(scene);

        stage.show();


        button.setOnMouseClicked(event -> {

            if (label.isVisible()) {

                label.setVisible(false);

            } else {

                Bounds sceneBounds = textField.localToScene(textField.getBoundsInLocal());

                label.setLayoutX((sceneBounds.getMinX() + (sceneBounds.getWidth() / 2)) - (label.getWidth() / 2));

                label.setLayoutY(sceneBounds.getMinY() - label.getHeight());

                label.setVisible(true);

            }

        });

    }

}


查看完整回答
反對 回復 2022-04-28
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

首先,您應該使用Bounds bounds = node.localToScreen(node.getBoundsInLocal());,因為可能不清楚相關Scene內容是什么。


然后調用tooltip.show(node, bounds.getMinX(), bounds.getMinY());應該給你一個工具提示,其左上角與節點的左上角相同。


現在,tooltip.show(node, bounds.getMinX() + nodeMiddle - tooltipMiddle, bounds.getMinY() - tooltipHeight);應該產生所需的結果,它確實適用于


double tooltipMiddle = (tooltip.getWidth()-18) / 2;

double tooltipHeight = tooltip.getHeight()-18;

double nodeMiddle = getWidth() / 2;

18 來自一個小實驗,我不確定為什么寬度和高度會偏離那個數量,但它似乎與工具提示中文本的長度或行數無關。


查看完整回答
反對 回復 2022-04-28
  • 2 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號