JavaFX自定义标签?

JavaFX自定义标签?,java,javafx,label,Java,Javafx,Label,我正在尝试创建一个自定义标签类型,它将包含一个“淡出”函数。这用于显示将闪现然后隐藏的消息 我正在使用Eclipse、SceneBuilder和Javafx。我不知道该怎么做,也不知道是否可能,但到目前为止我已经做到了: import javafx.scene.control.Label; import java.text.DecimalFormat; import java.util.Timer; import java.util.TimerTask; import javax.swing.

我正在尝试创建一个自定义标签类型,它将包含一个“淡出”函数。这用于显示将闪现然后隐藏的消息

我正在使用Eclipse、SceneBuilder和Javafx。我不知道该怎么做,也不知道是否可能,但到目前为止我已经做到了:

import javafx.scene.control.Label;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;


public class TimedMessage extends Label {

    private int countBy;
    private final String SUCCESS_COL = "#0bbf41";
    private final String FAIL_COL = "red";

    private int counter;

    private Timer messageTimer;

    public TimedMessage(int countBy) {
        this.countBy = countBy;
    }

    public void showMessage(boolean success, String message) {
        //function to show message
        /*
         *  To use: showMessage(false, "error"); or showMessage(true, "nice");
         *  Need:
         *  import javafx.scene.control.Label;
         *  import java.text.DecimalFormat;
         *  import java.util.Timer;
         *  import java.util.TimerTask;
         */

        this.setVisible(true); //show message label
        this.setOpacity(1); //set initial opacity to 1
        this.setText(message); //set the message's text
        if (success) {
            this.setStyle("-fx-text-fill: "+SUCCESS_COL);//set green
        }else {
            this.setStyle("-fx-text-fill: "+FAIL_COL);//set red
        }

        // Create new Timer
        messageTimer = new Timer();
        counter = 0; //should start from 0; do not change this value

        //messageTimer.scheduleAtFixedRate(messageTask, 5, 5);
         messageTimer.scheduleAtFixedRate(
                  new TimerTask() {
                      //timer task
                      private DecimalFormat deciFormat = new DecimalFormat("0.00");
                      public void run() {
                          if (counter >100){
                              //Stop timer
                              messageTimer.cancel();
                              messageTimer.purge();
                              this.setVisible(false); //hide message
                              this.setOpacity(1); //set initial opacity to 1
                              return;
                          }else {
                              double opacity = Double.valueOf(deciFormat.format((1.0 - counter/100.0))); //set opacity value
                              this.setOpacity(opacity); //set the opacity to the correct value
                              counter+=3;
                          }
                      }
                  }, countBy*100, countBy); //delay value, speed value

    }
}
这显然不起作用

这是我第一次在一个文件中处理凌乱的代码(因此,我尝试将代码从版本1拉入一个新的“对象”,我可以在多个类中使用它):

如果您有任何建议或帮助,我们将不胜感激。请尝试使用。下面的示例应用程序。应用程序从1变为0需要3秒钟

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @author rstein
 */
public class App extends Application {
    @Override
    public void start(final Stage primaryStage) {
        Label label = new Label("Label");

        FadeTransition ft = new FadeTransition(Duration.millis(3000), label);
        ft.setFromValue(1.0);
        ft.setToValue(0);
        ft.setCycleCount(1);
        ft.play();

        VBox root = new VBox(label);
        Scene scene = new Scene(root, 300, 200);

        primaryStage.setTitle(this.getClass().getSimpleName());
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    /**
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        Application.launch(args);
    }
}
尝试使用。下面的示例应用程序。应用程序从1变为0需要3秒钟

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @author rstein
 */
public class App extends Application {
    @Override
    public void start(final Stage primaryStage) {
        Label label = new Label("Label");

        FadeTransition ft = new FadeTransition(Duration.millis(3000), label);
        ft.setFromValue(1.0);
        ft.setToValue(0);
        ft.setCycleCount(1);
        ft.play();

        VBox root = new VBox(label);
        Scene scene = new Scene(root, 300, 200);

        primaryStage.setTitle(this.getClass().getSimpleName());
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    /**
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        Application.launch(args);
    }
}

你有什么问题?它不起作用吗?或者它没有按您希望的方式工作?查看
动画
API您有什么问题?它不起作用吗?或者它没有按您希望的方式工作?查看
动画
apiaw这太棒了!我想我的代码太复杂了。非常感谢你!啊,这太棒了!我想我的代码太复杂了。非常感谢你!