【问题标题】:JavaFX custom Label?JavaFX 自定义标签?
【发布时间】:2020-01-10 16:15:45
【问题描述】:

我正在尝试创建一个包含“淡出”功能的自定义标签类型。这是为了显示将闪烁然后隐藏的消息。

我正在使用 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;
        }
}

}

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

【问题讨论】:

  • 你的问题是什么?它不工作吗?或者它没有按照您希望的方式工作?
  • 查看Animation API

标签: java javafx label


【解决方案1】:

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

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);
    }
}

【讨论】:

  • 哇,这太棒了!我想我的代码太复杂了。非常感谢!
猜你喜欢
  • 2015-04-25
  • 2019-01-05
  • 2016-11-05
  • 2012-07-13
  • 2021-12-28
  • 2011-10-15
  • 2012-07-07
  • 2012-05-24
  • 1970-01-01
相关资源
最近更新 更多