2017-02-23 315 views
1

我遇到了麻烦,因此我的nameInput标签和passInput标签都以粗体显示。我的程序允许我做一个-fx-text-fill: #FFFFFF;,这是有效的,但是当我尝试-fx-font-weight: bold;时,它不会工作或在应用程序运行时加粗我的标签。Javafx CSS某些属性不起作用

下面是一个包含我的标签和按钮我的代码:

package appGUI; 

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 
import javafx.scene.layout.GridPane; 

public class Login extends Application { 

    Stage window; 
    Button loginButton; 
    String user = "test"; 
    String pw = "test"; 

    public static void main(String[] args) { 
     launch(args);  
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     window = primaryStage; 
     window.setTitle("Secret"); 

     GridPane grid = new GridPane(); 
     grid.setPadding(new Insets(10,10,10,10)); 
     grid.setVgap(8); 
     grid.setHgap(10); 

     // name label 
     Label nameLabel = new Label("Enter your name:"); 
     GridPane.setConstraints(nameLabel, 0, 0); 

     // name input 
     TextField nameInput = new TextField(); 
     nameInput.setPromptText("name"); 
     GridPane.setConstraints(nameInput, 1, 0); 

     // password label 
     Label passLabel = new Label("Enter the password:"); 
     GridPane.setConstraints(passLabel, 0, 1); 

     // password input 
     PasswordField passInput = new PasswordField(); 
     passInput.setPromptText("password"); 
     GridPane.setConstraints(passInput, 1, 1); 

     // login button 
     loginButton = new Button("Enter"); 
     GridPane.setConstraints(loginButton, 1, 2); 
     loginButton.setOnAction(e -> { 
      if(nameInput.getText().equals(user) && passInput.getText().equals(pw)) { 
       System.out.println("THIS WORKS. YOU FINALLY SOLVED IT!!!!"); 
      } else { 
       AlertBox.display("Error: Access Denied", "Only a special person can access this app"); 
      } 

     }); 

     grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, loginButton); 

     Scene scene = new Scene(grid, 350, 200); 
     scene.getStylesheets().add("/appGUI/custom.css"); 

     window.setScene(scene); 
     window.show(); 

    } 
} 

这里的题目是“custom.css”可以正常工作和做的一切,我除了粗体字写的.css文件:

.root { 
    -fx-background-color: linear-gradient(#707070, #BABABA); 
} 

.label { 
    -fx-text-fill: #FFFFFF; 
    -fx-font-weight: bold; 
} 

.button { 
    -fx-background-color: linear-gradient(#00F5FF, #FFA07A); 
    -fx-background-radius:10; 
} 

我已经尝试了几乎所有的东西,我似乎无法找到答案,看起来像是一个很容易解决的问题。我的标签不会大胆,但他们会填写#FFFFFF,我只想知道为什么一个物业的作品,而另一个不行!

+0

它运行良好,没有什么是错的。尝试“大胆”。使用FXForms2生成的表单有 – DVarga

+0

也有同样的问题。有些属性起作用,有些则不起作用。在我的情况下,大小和斜体工作正常,而文字填充颜色不... – user978548

回答

0

你的代码似乎很好,它应该工作。即使它不能以这种方式工作,你也可以用java代码替代你的标签加粗

nameInput.setFont(Font.font("Serif", FontWeight.BOLD, 20)); 
passInput.setFont(Font.font("Serif", FontWeight.BOLD, 20)); 
0

由于一些奇怪的原因,默认字体不一定支持所有功能。

尝试:

.label { 
    -fx-font-family: "Helvetica"; 
    -fx-font-weight: bold; 
} 

如果你想使这个改变应用广泛,它添加到场景:

.root { 
    -fx-font-family: "Helvetica"; 
} 

scene.getStylesheets().add("pathTo/root.css");