如何解决如何在没有警告的情况下在 JavaFX 中使用自定义 CSS?
我正在为标签制作自定义样式,但是当我调用它时,我收到以下警告 2021 年 2 月 6 日下午 7:54:12 com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged 警告:找不到资源“@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));
//Cascading Style Sheet (CSS)
scene.getStylesheets().add(getClass().getResource("../Style.css").toExternalForm());
Color foreground = Color.rgb(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.55));
background.setstroke(foreground);
background.setstrokeWidth(1.5);
VBox vBox = new VBox(5);
vBox.setPadding(new Insets(10,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,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,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,0.8);
}
任何建议。
解决方法
您的 CSS 选择器 .field-background
是一个 class 选择器。
因此,您应该在 userName
实例上使用 getStyleClass。
userName.getStyleClass().add("field-background");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。