【问题标题】:JavaFX ignoring backgrounds and fontweightsJavaFX 忽略背景和字体粗细
【发布时间】:2017-10-04 18:33:38
【问题描述】:

我正在学习基本的 Java 和 JavaFX。我正在尝试将某些样式应用于一个简单的程序。我已经成功连接了 CSS 和一些样式的工作,而其他的则被忽略了。因此,我特别遇到了 -fx-background-color 和 -fx-font-weight 的问题。 CSS如下:

.label{
-fx-font-size: 16px;
-fx-font-weight: bold;
-fx-font-family: Tarazedi;
-fx-text-fill: #FF9900;    
}

#h1bar {
-fx-text-fill: #000000;
-fx-effect: dropshadow( gaussian, #000000, 1, 0, 1, 1 );
-fx-background-color: linear-gradient( to right,
                             rgba(255, 153, 0, 1) 0px,
                             rgba(255, 153, 0, 1) 16px,
                             rgba(171, 153, 0, 0.67) 32px,
                             rgba(171, 153, 0, 0.67) 480px,
                             rgba(255, 153, 0, 0.33) 720px,
                             rgba(255, 153, 0, 0) 960px
                           );
-fx-padding: 4px;
-fx-background-radius: 16px;
}

Java如下:

    primaryStage.setTitle("Welcome to tarazedi.com!");                      // Define window and title bar.
    GridPane grid = new GridPane();                                         // Create a GridPane

    grid.setHgap(16);                                                       // Set 10px gridlines.
    grid.setVgap(16);
    grid.setPadding(new Insets(32, 32, 32, 32));                            // TRBL padding.

    Label scenetitle = new Label("\uf0a3  Welcome to tarazedi.com!");
    scenetitle.setId("h1bar");
    grid.add(scenetitle, 0, 0, 2, 1);

    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((ActionEvent e) -> {
        actiontarget.setFill(Color.FIREBRICK);
        actiontarget.setText("Sign in button pressed");
    });

    Scene scene = new Scene(grid, 400, 400);                                // Grid is the root node.
    primaryStage.setScene(scene);                                           // Display it.
    scene.getStylesheets().add(VictorSheckelsFX.class.getResource("VictorStyle.css").toExternalForm());
    primaryStage.show();

预期的行为会导致标题栏看起来与我网站上的橙色栏大致相似:http://victorsheckels.com/。相反,我的字体上有实心橙色条,没有阴影,也没有粗体。字体大小、系列和颜色是 WAI,内边距和半径也是。

【问题讨论】:

  • 如果您使用的是 Java 8,您可能会尝试将 CSS 记录器 com.sun.javafx.Logging.getCSSLogger() 设置为显示所有消息。也可以通过日志属性文件来完成。

标签: css javafx background


【解决方案1】:

使用百分比而不是像素。正如here 报告的那样,JavaFX 不支持非百分比止损

例如:

-fx-background-color: linear-gradient(to right,
rgba(255, 153, 0, 1) 0%,
rgba(255, 153, 0, 1) 1.6%,
rgba(171, 153, 0, 0.67) 3.3%,
rgba(171, 153, 0, 0.67) 50%,
rgba(255, 153, 0, 0.33) 75%,
rgba(255, 153, 0, 0) 100%);

【讨论】:

  • 谢谢,解决了背景色问题。 :)
  • 字体粗细和阴影似乎也可以工作,对吧?删除它们时我可以清楚地看到不同
  • 无论哪种方式,我都看不到明显的区别。当我将它们移动到另一个元素时,它也不会改变。
  • 好吧,这很奇怪。这是删除字体粗细normal 并将其设置为粗体bold 时的外观
猜你喜欢
  • 2017-04-05
  • 2012-11-25
  • 2014-03-26
  • 2020-09-23
  • 2013-01-31
  • 1970-01-01
  • 2015-10-06
  • 2011-08-27
  • 2017-01-20
相关资源
最近更新 更多