JavaFX自定义标签?

问题描述 投票:1回答:1

我正在尝试创建一个自定义Label类型,其中将包含一个用于“淡出”功能。这用于显示将先闪烁然后被隐藏的消息。

我正在使用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放入一个我可以在多个类中使用的新“对象”中:]]

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

import java.io.IOException;
import java.sql.SQLException;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Window;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

public class PrimaryController {
...
    @FXML
    private Label messageLabel;


    private int counter;
    private Timer messageTimer;
    private 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;
         */

        messageLabel.setVisible(true); //show message label
        messageLabel.setOpacity(1); //set initial opacity to 1
        messageLabel.setText(message); //set the message's text
        if (success) {
            messageLabel.setStyle("-fx-text-fill: #0bbf41");//set green
        }else {
            messageLabel.setStyle("-fx-text-fill: red");//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();
                              messageLabel.setVisible(false); //hide message
                              messageLabel.setOpacity(1); //set initial opacity to 1
                              return;
                          }else {
                              double opacity = Double.valueOf(deciFormat.format((1.0 - counter/100.0))); //set opacity value
                              messageLabel.setOpacity(opacity); //set the opacity to the correct value
                              counter+=3;
                          }
                      }
                  }, 300, 4); //delay value, speed value

    }

    ...
    @FXML
    void logInOnClick() throws IOException, SQLException {
        ...
        if (userName.getText().isEmpty()) {
            showMessage(false, "error in adsadsasdasdadsdasasd");
            //showAlert(Alert.AlertType.ERROR, owner, "Form Error!","Please enter your email id");
            return;
        }
}

}

任何建议或帮助,将不胜感激,谢谢。

我正在尝试创建一个自定义Label类型,其中将包含一个用于“淡出”功能。这用于显示将先闪烁然后被隐藏的消息。我正在使用Eclipse,SceneBuilder和...

java javafx label
1个回答
4
投票

尝试使用FadeTransition。下面的示例应用程序。该应用需要三秒钟才能从1变为0透明度。

© www.soinside.com 2019 - 2024. All rights reserved.