简体   繁体   English

JAVAFX还有什么其他方法可以使“标签”闪烁而不是使用时间轴?

[英]JAVAFX What other ways are there to make a Label blink, instead of using the timeline?

How do I create the blinking effect on my custom label other than using the timer? 除了使用计时器外,如何在自定义标签上创建闪烁效果?

Here's a workable example I am using... 这是我正在使用的可行示例...

The custom label class 自定义标签类

public class CustomLabel extends Label{

   public CustomLabel(String text1){
      super(text1);
      setAlignment(Pos.CENTER);

      //for blinking effect
        TIMER = new Timeline(
            new KeyFrame(Duration.seconds(1),ae->ChangeColor1()),
            new KeyFrame(Duration.seconds(2),ae->ChangeColor2())
        );
        TIMER.setCycleCount(Animation.INDEFINITE);
        TIMER.play();
    }

    public void ChangeColor1(){
        setTextFill(Color.Pink);    
    }

    public void ChangeColor2(){
        setTextFill(Color.Blue);    
    }

}

Main class 主班

StackPane sp = new StackPane(new CustomLabel("Testing"));
Scene sc = new Scene(sp)
primaryStage.setScene(sc)
sc.show();

Is there any way to make this method "lighter"? 有什么方法可以使此方法“更轻”?

Somehow this method causes an error after keeping the app running for 1 full day 在保持应用程序运行1天后,此方法以某种方式导致错误

java.lang.NullPointerException
Exception in thread "JavaFX Application Thread" java.lang.OutOfMemoryError: Java heap space

Any advise on how to get around this? 任何建议如何解决这个问题? This app is required to be running for more than 1 full day. 此应用必须运行超过1天。

to reply to your above answer, (as the comments do not allow for code blocks) this is the way you will need to do it for it to be able to "blink": 回答您的上述答案(因为注释不允许使用代码块),这是您需要执行的操作才能使其“闪烁”:

KeyFrame kfP = new KeyFrame(Duration.seconds(1), new KeyValue(ae.textFillProperty(), Color.PINK));
Timeline TIMER = new Timeline();
TIMER.getKeyFrames().add(kfP);
TIMER.setCycleCount(Animation.INDEFINITE);
TIMER.setAutoReverse(true);
TIMER.play();   

The reason to do it this way is you only create one keyframe and it only uses the one keyframe, this stops the timeline from creating multiple new keyframes as it would in your initial code and as the timeline continues to run it does not get Garbage collected so it just builds until your out of memory. 这样做的原因是,您只能创建一个关键帧,而仅使用一个关键帧,这将阻止时间线像在初始代码中那样创建多个新的关键帧,并且随着时间线的继续运行,不会收集到垃圾因此它会一直构建到内存不足为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM