英文:
How to add Tooltips to button in an anchorpane?
问题
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="800.0" prefWidth="1855.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calibre.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="Add Books">
<tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>
<Label fx:id="label" layoutX="6.0" layoutY="7.0" minHeight="16" minWidth="69" />
<Button layoutX="274.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleEditMetadataButtonAction" prefHeight="32.0" prefWidth="200.0" stylesheets="@style.css" text="Edit Metadata" />
<Button layoutX="564.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleFetchNewsButtonAction" prefHeight="32.0" prefWidth="200.0" text="Fetch News" />
<Button layoutX="839.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleRemoveButtonAction" prefHeight="32.0" prefWidth="200.0" text="Remove" />
<Button layoutX="1225.0" layoutY="9.0" mnemonicParsing="false" onAction="#handleLibraryButtonAction" prefHeight="32.0" prefWidth="200.0" text="Library" />
<MenuButton layoutX="1607.0" layoutY="11.0" minWidth="200.0" mnemonicParsing="false" text="MenuButton" textFill="#eeeeee">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="747.0" layoutY="166.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/1.png" />
</image>
</ImageView>
<ScrollBar layoutX="54.0" layoutY="773.0" prefHeight="18.0" prefWidth="1781.0" />
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="149.0" layoutY="180.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/4.jpg" />
</image>
</ImageView>
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="1325.0" layoutY="159.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/3.png" />
</image>
</ImageView>
<Button layoutX="382.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadHemingwayButtonAction" onDragOver="#handleReadBookHoverTip" text="Read" />
<Button layoutX="985.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadGentlemanButtonAction" text="Read" />
<Button layoutX="1580.0" layoutY="731.0" mnemonicParsing="false" onAction="#handleReadFlowersButtonAction" text="Read" />
</children>
</AnchorPane>
在锚点面板中,将工具提示添加到按钮上的代码已经在你的FXML文件中。如果您遇到工具提示在锚点面板中不起作用的问题,可能是由于某些其他因素引起的。检查您的代码是否有错误,确保在FXML控制器中正确处理按钮的handleAddBookButtonAction
动作,以便工具提示能够显示。同时,锚点面板本身并不影响工具提示的显示
英文:
I have some buttons inside an anchorpane and want to know how I can add Tooltips to a button.
This is my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="800.0" prefWidth="1855.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calibre.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="Add Books" />
<tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>
<Label fx:id="label" layoutX="6.0" layoutY="7.0" minHeight="16" minWidth="69" />
<Button layoutX="274.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleEditMetadataButtonAction" prefHeight="32.0" prefWidth="200.0" stylesheets="@style.css" text="Edit Metadata" />
<Button layoutX="564.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleFetchNewsButtonAction" prefHeight="32.0" prefWidth="200.0" text="Fetch News" />
<Button layoutX="839.0" layoutY="7.0" mnemonicParsing="false" onAction="#handleRemoveButtonAction" prefHeight="32.0" prefWidth="200.0" text="Remove" />
<Button layoutX="1225.0" layoutY="9.0" mnemonicParsing="false" onAction="#handleLibraryButtonAction" prefHeight="32.0" prefWidth="200.0" text="Library" />
<MenuButton layoutX="1607.0" layoutY="11.0" minWidth="200.0" mnemonicParsing="false" text="MenuButton" textFill="#eeeeee">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="747.0" layoutY="166.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/1.png" />
</image></ImageView>
<ScrollBar layoutX="54.0" layoutY="773.0" prefHeight="18.0" prefWidth="1781.0" />
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="149.0" layoutY="180.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/4.jpg" />
</image>
</ImageView>
<ImageView fitHeight="564.0" fitWidth="358.0" layoutX="1325.0" layoutY="159.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Books/3.png" />
</image>
</ImageView>
<Button layoutX="382.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadHemingwayButtonAction" onDragOver="#handleReadBookHoverTip" text="Read" />
<Button layoutX="985.0" layoutY="722.0" mnemonicParsing="false" onAction="#handleReadGentlemanButtonAction" text="Read" />
<Button layoutX="1580.0" layoutY="731.0" mnemonicParsing="false" onAction="#handleReadFlowersButtonAction" text="Read" />
</children>
</AnchorPane>
But it says tooltips don't exist in Anchorpanes.
Does this mean I need to change from an Anchorpane, if so what are my alternatives.
I'm doing this in Netbeans.
Warm Regards
答案1
得分: 0
如果您有控制器类,您可以在initialize
方法内从该方法中获取tooltip
。
yourBtnId.setTooltip(new Tooltip("添加客户"));
如果您正在使用XML
编辑,
<Button>
<tooltip>
<Tooltip textAlignment="CENTER" text="你好" />
</tooltip>
</Button>
在您的XML
文件中,删除所有的button
标签,然后粘贴如下内容:
<Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="添加图书">
<tooltip>
<Tooltip text="我的工具提示" />
</tooltip>
</Button>
您的错误是Button
标签在添加工具提示/>
之前已经关闭。
英文:
if you have controller class you can get tooltip
from this method inside the initialize
method
yourBtnId.setTooltip(new Tooltip("Add Customer"));
If you are using XML
editing
<Button>
<tooltip><Tooltip textAlignment="CENTER" text="HELLO" /></tooltip>
</Button>
in your XML
file remove your all button
tag and paste this
<Button fx:id="button" layoutX="6.0" layoutY="7.0" onAction="#handleAddBookButtonAction" prefHeight="32.0" prefWidth="200.0" text="Add Books">
<tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>
your mistake is Button
tag is closing before you add the tooltip />
专注分享java语言的经验与见解,让所有开发者获益!
评论