javafx和css如何使用变量

时间:2015-11-27 16:23:18

标签: css javafx fxml

我有一个登录屏幕,当你没有填写正确的用户名和pw用户名和密码时,文字字段将闪红色,然后需要在一定时间内淡化回白色(可能是1或2秒)。然而,最大的问题是我不知道如何在css中实现变量。这是我必须改变的颜色

package com.cortex.gui;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;

public class FXMLController {
Stage stage;
Parent root;
double fade = 1;
Timeline borderFade = new Timeline(new KeyFrame(Duration.millis(100), ae -> faultLine(fade)));
//login screen objects
@FXML    private Button loginButton;
@FXML    private TextField usernameField;
@FXML    private PasswordField passwordField;
//main screen objects
@FXML    private Button returnButton;

@FXML
protected void handleLoginButtonAction(ActionEvent event) throws IOException     {
    if (passwordField.getText().equals("password") && usernameField.getText().equals("username")) {
        if (event.getSource() == loginButton) {
            stage = (Stage) loginButton.getScene().getWindow();
            sceneSetter("/InterfaceMain.fxml");
        }
    }
    else {
        borderFade.setCycleCount(10);
        borderFade.play();
    }
}

protected void faultLine(double i){
    usernameField.setStyle("-fx-background-color: rgb(220, 60, 20, fade)");
    passwordField.setStyle("-fx-background-color: rgba(220, 60, 20, fade)")
    fade = fade - 0.1;
}
}

我想为它使用淡入淡出变量,但是它给出了一个解析错误,因为java似乎没有把它识别为double。

2 个答案:

答案 0 :(得分:0)

setStyle' s参数是一个String,所以你想要的是一个包含你的双"淡出"的值的String。当将double与String连接时,Java会自动执行此操作。

protected void faultLine(double i){
    usernameField.setStyle("-fx-background-color: rgb(220, 60, 20, " + fade + ")");
    passwordField.setStyle("-fx-background-color: rgba(220, 60, 20, " + fade + ")");
    fade = fade - 0.1;
}

答案 1 :(得分:0)

您可以将double转换为字符串,然后将其连接为

  private void ValidateButton_Click(object sender, EventArgs e)
        {

            foreach (var control in this.Controls)
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                           //CODE GOES HERE to
                           //Process checked in background
                           //and then
                           validatebutton.hide();
                           nextbutton.show();
                    }  
                }
                else
                {
                    DialogResult uncheckederror = MessageBox.Show("You must select at least one checkbox",
                        "Validation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }

您也可以使用protected void faultLine(double i){ usernameField.setStyle("-fx-background-color: rgb(220, 60, 20," +String.valueOf(fade)+")"); passwordField.setStyle("-fx-background-color: rgba(220, 60, 20,"+String.valueOf(fade)+")"); fade = fade - 0.1; }