简体   繁体   English

JavaFx CSS无法与Eclipse一起使用

[英]JavaFx Css not working with eclipse

I'm new to JavaFx and I'm trying to set up a style. 我是JavaFx的新手,正在尝试建立一种样式。 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. 无论我在CSS文件中做什么,都不会对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. 我正在使用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. 奇怪的是它确实看到了CSS文件,我知道这是因为如果我将其更改为不存在的文件,它将无法编译。 I've try a few code for the CSS file but at the moment it looks like this 我已经为CSS文件尝试了一些代码,但目前看起来像这样

.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. 我遵循相同的Oracle教程,并且具有与Eclipse相同的症状。 Had you check that there is only one "login2.css" file in your project and that it is located in the bin folder. 您是否检查过项目中只有一个“ login2.css”文件,并且该文件位于bin文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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