JavaFx Css not working with eclipse

谁都会走 提交于 2019-12-25 02:29:33

问题


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;
}

回答1:


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");



回答2:


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.



来源:https://stackoverflow.com/questions/24564973/javafx-css-not-working-with-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!