如何在没有警告的情况下在 JavaFX 中使用自定义 CSS?

How to use the custom CSS in JavaFX without getting warning?

我正在为标签制作自定义样式,但在调用它时收到以下警告 2021 年 2 月 6 日7:54:12下午com.sun.javafx.css.StyleManagerloadStylesheetUnPrivileged 警告:找不到资源“@Field-background”。 这不会使我的代码获得自定义 CSS 效果。

我的代码:

public class Tutorial14 extends Application {

Connection conn;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;


@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFX 8 Tutorial 14 - SQLite");
    CheckConnection();



    // Create transparent stage
    primaryStage.initStyle(StageStyle.TRANSPARENT);

    Group root = new Group();
    Scene scene = new Scene(root, 220, 210, Color.rgb(0, 0, 0, 0));
    //Cascading Style Sheet (CSS)
    scene.getStylesheets().add(getClass().getResource("../Style.css").toExternalForm());
    Color foreground = Color.rgb(255, 255, 255, 0.9);

    //Rounded Rectangular where we will add our components
    //Rectangular Background
    Rectangle background = new Rectangle(220, 210);
    background.setX(0);
    background.setY(0);
    background.setArcHeight(15);
    background.setArcWidth(15);
    background.setFill(Color.rgb(0, 0, 0, 0.55));
    background.setStroke(foreground);
    background.setStrokeWidth(1.5);

    VBox vBox = new VBox(5);
    vBox.setPadding(new Insets(10, 0, 0, 10));
    Label label = new Label("Login Status");
    //label.setTextFill(Color.WHITESMOKE);
    label.setFont(new Font("SanSerif", 20));

    TextField userName = new TextField();
    userName.setFont(Font.font("SanSerif", 20));
    userName.setPromptText("User Name");

    userName.getStylesheets().add("@Field-background");

    userName.setMaxWidth(200);


    PasswordField password = new PasswordField();
    password.setFont(Font.font("SanSerif", 20));
    password.setPromptText("Password");
    password.setMaxWidth(200);

    Button button = new Button("Login");
    button.setFont(Font.font("SanSerif, 15"));
    button.setMaxWidth(200);


    password.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });
    userName.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });
    button.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });


    vBox.getChildren().addAll(label, userName, password, button);
    root.getChildren().addAll(background, vBox);
    primaryStage.setScene(scene);
    primaryStage.show();

}

public void CheckConnection() {
    conn = SqlConnection.DbConnector();
    if (conn == null) {
        System.out.println("Connection Not Successful");
        System.exit(1);
    } else {
        System.out.println("Connection Successful");
    }
}

}

我的CSS

.label {
-fx-text-fill: whitesmoke;
}

.button {
-fx-padding: 10 20 10 20;
-fx-end-margin: 200;
}

.field-background {
-fx-text-fill: black;
-fx-prompt-text-fill: gray;
-fx-highlight-fill: gray;
-fx-highlight-text-fill: black;
-fx-background-color: rgba(255, 255, 255, 0.8);
}

任何建议。

您的 CSS 选择器 .field-backgroundclass 选择器。
因此,您应该在 userName 实例上使用 getStyleClass

userName.getStyleClass().add("field-background");