【发布时间】: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;
}
}
}
任何建议或帮助将不胜感激,谢谢。
【问题讨论】:
-
你的问题是什么?它不工作吗?或者它没有按照您希望的方式工作?
-
查看
AnimationAPI