我在这里做了一个方便的小方法,用于在GUI中的给定坐标处生成浮动文本。
public static void spawnLabel(final String text, final Color color, final double x, final double y) {
final Popup pc = new Popup();
final Label label = new Label(text);
pc.getScene().setRoot(new AnchorPane(label));// Add label to Popup
/* Style label */
label.setTextFill(color);
label.setBackground(null);
label.setStyle("-fx-font-weight: bold;");
/* Set Popup positions */
pc.setX(0);
pc.setY(0);
final TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(2), label);
translateTransition.setFromY(y);
translateTransition.setFromX(x);
translateTransition.setToX(x);
translateTransition.setToY(y - 25);
translateTransition.setInterpolator(Interpolator.EASE_OUT);
final FadeTransition opacityTransition = new FadeTransition(Duration.seconds(2), label);
opacityTransition.setFromValue(0.7);
opacityTransition.setToValue(0.0);
pc.show(stage);// Uses the Stage of my app.
translateTransition.play();
opacityTransition.play();
opacityTransition.setOnFinished(event -> pc.hide());
}
它只需要一些文本,一种颜色和一个位置,并在任何位置产生一段文字。文本将具有给定的颜色,并将说明传递给此方法的任何内容。这一切都很好,并使用Popup
对象作为窗口,Label
对象作为文本节点。文本将慢慢向上移动并逐渐消失。
这是显示文字的图片:
我遇到的问题是,当多次调用该方法时,程序开始“延迟”#34;。 (通常4-5个电话就足以使"滞后"重。)我的意思是"滞后"是某些功能,如按下按钮或悬停在某些节点上,会大大延迟。
对于制作,我打算非常密集地使用这种方法,因此我无法承受常见功能的轻微延迟。 我不知道导致这种滞后的原因以及如何摆脱它而且我还没有在谷歌上找到任何东西,所以我来到这里。
编辑1:在我的GUI中通过Button
节点反复调用我的方法后,随机发生以下异常并且我的悬停文本停止显示:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.stage.Window$TKBoundsConfigurator.apply(Window.java:1274)
at javafx.stage.Window$TKBoundsConfigurator.pulse(Window.java:1290)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:378)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.PaintCollector.liveRepaintRenderJob(PaintCollector.java:320)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$ViewEventNotification.run(GlassViewEventHandler.java:831)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$ViewEventNotification.run(GlassViewEventHandler.java:792)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleViewEvent$368(GlassViewEventHandler.java:871)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleViewEvent(GlassViewEventHandler.java:870)
at com.sun.glass.ui.View.handleViewEvent(View.java:539)
at com.sun.glass.ui.View.notifyResize(View.java:875)
at com.sun.glass.ui.win.WinWindow._setBounds(Native Method)
at com.sun.glass.ui.Window.setBounds(Window.java:572)
at com.sun.javafx.tk.quantum.WindowStage.setBounds(WindowStage.java:317)
at javafx.stage.Window$TKBoundsConfigurator.apply(Window.java:1274)
at javafx.stage.Window$TKBoundsConfigurator.pulse(Window.java:1290)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:378)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
编辑2:我还没有找到完全导致这种延迟的原因,但我找到了一种让我摆脱它的解决方法。我发现当我的方法第四行的Pane
调用setRoot(...)
中存在任何类型的AnchorPane
时,滞后仍然存在。我在label
使用label
作为我AnchorPane
的包装,因为Popup
在直接传入时不会显示。当我删除AnchorPane
时,滞后消失了。
无论如何,经过多次尝试,我得到的文字显示在/**
* Spawns a floating piece of text that flies upwards a little then
* disappears. The source point of the text is specified via the {@code x}
* and {@code y} parameters.
*
* @param text
* The text to render.
* @param color
* The color of the rendered text.
* @param x
* The starting x position of the text.
* @param y
* The starting y position of the text.
*/
public static void spawnLabel(final String text, final Color color, final double x, final double y) {
final Popup pc = new Popup();
final Label label = new Label(text);
final TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(2), label);
final FadeTransition opacityTransition = new FadeTransition(Duration.seconds(2), label);
pc.getScene().setRoot(label);
/* Style label */
label.setTextFill(color);
label.setBackground(null);
label.setStyle("-fx-font-weight: bold;");
/* Set Popup positions */
pc.setX(x);
pc.setWidth(label.getMaxWidth());
pc.setY(y - 50);
/* Build transitions */
translateTransition.setFromY(30);
translateTransition.setFromX(0);
translateTransition.setToX(0);
translateTransition.setToY(5);
translateTransition.setInterpolator(Interpolator.EASE_OUT);
opacityTransition.setFromValue(0.7);
opacityTransition.setToValue(0.0);
opacityTransition.setOnFinished(e -> pc.hide());
/* Show the Popup */
pc.show(stage);
pc.setHeight(50);
/* Play the transitions */
translateTransition.play();
opacityTransition.play();
}
而没有{{1}}包装。我的方法现在如下:
{{1}}