简体   繁体   中英

JavaFx Css not working with eclipse

I'm new to JavaFx and I'm trying to set up a style. I was flowing a tutorial but I hit a wall I can't get pass. No matter what I do in the CSS file it as no effect on the App.

public class LoginForm extends Application{

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

@Override
public void start(Stage primaryStage){
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25,25,25,25));

    Scene scene =new Scene(grid,300,275);
    scene.getStylesheets().add(getClass().getClassLoader().getResource("login2.css").toExternalForm());  



    Label userName = new Label("User Name:");
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    Label pw = new Label("Password:");
    grid.add(pw, 0, 2);         
    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);  
    Button btn =new Button("Sign in");
    HBox hbBtn =new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn,1,4);



    final Text actiontarget =new Text();
            grid.add(actiontarget, 1, 6);

  btn.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent e){
        actiontarget.setFill(Color.FIREBRICK);
        actiontarget.setText("Sign in button pressed");
    }           
    });                         
grid.getStylesheets().add("login2.css");

primaryStage.setScene(scene);
primaryStage.show();
}}

I'm using Eclipse, Java 7.1. The weird thing is it does see the CSS file, and I know this because if I change it to a file that isn't there it will not compile. I've try a few code for the CSS file but at the moment it looks like this

.root{
    -fx-font-size: 14pt;
    -fx-font-family: "Tahoma";
    -fx-base: #DFB951;
    -fx-background: #A78732;
    -fx-focus-color: #B6A678;
}

It is possible to add the Cascading Style Sheet to the Scene. So if it doesn't solve the problem you can also set the style in the code for a first try.

This is possible to do it like that:

Component.setStyle("CSS-Code insert");

I was following the same Oracle tutorial and had the same symptom whith Eclipse. Had you check that there is only one "login2.css" file in your project and that it is located in the bin folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM