JavaFX工具提示-边框颜色错误
我对JavaFX中的工具提示有问题。为了说明这个问题的核心,我通过IntelliJ Idea创建了新的JavaFX项目。该项目的根窗格是带有JavaFX工具提示-边框颜色错误,java,css,javafx,tooltip,border,Java,Css,Javafx,Tooltip,Border,我对JavaFX中的工具提示有问题。为了说明这个问题的核心,我通过IntelliJ Idea创建了新的JavaFX项目。该项目的根窗格是带有alignment=“CENTER”的HBox,该HBox包含四个按钮(文本:按钮A-D)。每个按钮都用文本设置了工具提示:示例工具提示A-B。通过fxml文件,我还加载了名为styles.css的文件中保存的级联样式 问题描述:我启动应用程序,当我将鼠标悬停在第一个按钮上时,它的工具提示与CSS文件描述的完全相同。当我在第二个按钮上悬停时,工具提示的右边框
alignment=“CENTER”
的HBox,该HBox包含四个按钮(文本:按钮A-D)。每个按钮都用文本设置了工具提示:示例工具提示A-B。通过fxml文件,我还加载了名为styles.css的文件中保存的级联样式
问题描述:我启动应用程序,当我将鼠标悬停在第一个按钮上时,它的工具提示与CSS文件描述的完全相同。当我在第二个按钮上悬停时,工具提示的右边框会稍微亮一点。但当我将鼠标悬停在第三个或第四个按钮上时,工具提示的右红色边框略微可见,而我们可以看到它的背景(黄色)和边框颜色的混合。其他边界都没有这样做,只有正确的边界这很奇怪。
我尝试的内容:我尝试更改字体系列、字体大小和填充
我的问题:是什么导致了这个问题?你有解决那个问题的办法吗?你也有这个麻烦吗
屏幕截图显示了我描述的问题
*鼠标光标悬停在按钮C上
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
public class Controller {
}
Controller.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
public class Controller {
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox alignment="CENTER" spacing="10.0" stylesheets="@styles.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" text="Button A">
<tooltip>
<Tooltip text="Sample tooltip A" />
</tooltip>
</Button>
<Button mnemonicParsing="false" text="Button B">
<tooltip>
<Tooltip text="Sample tooltip B" />
</tooltip>
</Button>
<Button mnemonicParsing="false" text="Button C">
<tooltip>
<Tooltip text="Sample tooltip C" />
</tooltip>
</Button>
<Button mnemonicParsing="false" text="Button D">
<tooltip>
<Tooltip text="Sample tooltip D" />
</tooltip>
</Button>
</children>
</HBox>
PS:对不起我的英语如果这是JavaFX的错误,那么解决问题的关键是解决问题我设法重现了您的问题,因此我一直在试图找出这种奇怪/错误行为的原因 首先,我认为这和文本的长度有关,因为“C”和“A”的长度不同 出于调试目的,我用代码创建了场景
@Override
public void start(Stage primaryStage) throws IOException {
Button bA=new Button("Button A");
Tooltip tA=new Tooltip("Sample tooltip A");
tA.widthProperty().addListener((obs,b,b1)->System.out.println("A: "+tA.getWidth()));
bA.setTooltip(tA);
Button bB=new Button("Button B");
Tooltip tB=new Tooltip("Sample tooltip B");
tB.widthProperty().addListener((obs,b,b1)->System.out.println("B: "+tB.getWidth()));
bB.setTooltip(tB);
Button bC=new Button("Button C");
Tooltip tC=new Tooltip("Sample tooltip C");
tC.widthProperty().addListener((obs,b,b1)->System.out.println("C: "+tC.getWidth()));
bC.setTooltip(tC);
Button bD=new Button("Button D");
Tooltip tD=new Tooltip("Sample tooltip D");
tD.widthProperty().addListener((obs,b,b1)->System.out.println("D: "+tD.getWidth()));
bD.setTooltip(tD);
HBox root =new HBox(10,bA,bB,bC,bD);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("root.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
在这里,我为工具提示宽度的更改添加了一个侦听器
这是输出:
A: 18.0
A: 0.0
A: 95.16523742675781
A: 95.0
A: 95.16523742675781
A: 95.0
B: 18.0
B: 0.0
B: 94.43310546875
B: 94.0
B: 94.43310546875
B: 94.0
C: 18.0
C: 0.0
C: 94.9012680053711
C: 94.0
C: 94.9012680053711
C: 94.0
D: 18.0
D: 0.0
D: 95.73799133300781
D: 95.0
D: 95.73799133300781
D: 95.0
你注意到了吗,每个工具提示都得到了它的真实宽度,然后被截断了
这意味着“A”的工具提示从95.17到95被截断了几次,并且没有可见的指示,但是“C”从94.90到94被截断了,几乎是1个像素,所以右边框几乎不可见
我说不出这种行为的原因。但我们可以尝试解决它
一个简单的解决方案是为文本提供足够的空间。在FXML文件中,这将解决以下问题:
<Tooltip prefWidth="100" text="Sample tooltip A" />
...
<Tooltip prefWidth="100" text="Sample tooltip C" />
...
现在,对于每个按钮:
tC.widthProperty().addListener((obs,b,b1)->setTooltipWidth(tC));
如果我们再次打印宽度变化,现在将在按钮“C”上显示:
并且工具提示将正确显示:
尝试使边框透明:
.tooltip{
-fx-text-fill: black;
-fx-background-radius: 0px;
-fx-background-color: transparent,red,yellow;
-fx-background-insets: 0, 1, 2;
-fx-effect: null;
}
很奇怪。你的解决方案有效,我几分钟前试过了。但这个解决方案有点让人崩溃。。。如果明天才有更好的解决办法,我接受你的解决办法。谢谢。通过使用查找功能,我简化了设置宽度的函数,但无论如何,这肯定是一个解决办法。无论如何,JavaFX可能会在一段时间内让我大吃一惊!:)如果你愿意,看看我的另一个问题,你会明白的。
.tooltip{
-fx-text-fill: black;
-fx-background-radius: 0px;
-fx-background-color: transparent,red,yellow;
-fx-background-insets: 0, 1, 2;
-fx-effect: null;
}