在JavaFX中,我应该使用CSS还是setter方法来更改UI节点上的属性? [英] In JavaFX, should I use CSS or setter methods to change properties on my UI Nodes?

查看:109
本文介绍了在JavaFX中,我应该使用CSS还是setter方法来更改UI节点上的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以在我的UI节点上设置属性(标签,按钮等)。

There are two ways to set properties on my UI nodes (labels, buttons, etc.)

我可以使用这样的东西:

I could use something like this:

Button b = new Button("Sample");
b.setTextFill( Paint.valueOf("red") );
b.setTextAlignment( TextAlignment.CENTER );

或者我可以做同等的事情:

Or I could do something equivalent:

Button b = new Button("Sample");
b.setStyle("-fx-text-alignment: center; -fx-text-fill: red");

我想知道的是,这些方法之一是出于任何原因(个人口味除外) )?

What I'm wondering is, is one of these methods preferable for any reason (other than personal taste)?

推荐答案

这个问题的答案可以归结为个人品味,但是我有一些一般的经验法则认为很有用。

The answer to this does come down to personal taste a little bit, but there are some general rules of thumb things which I think are useful.

JavaFX中样式的四个选项是:

The four options for styling in JavaFX are:


  1. css样式表。

  2. 从代码发出的css setStyle命令。

  3. 样式的java代码。

  4. fxml属性造型。

  1. css stylesheets.
  2. css setStyle command issued from code.
  3. java code for styling.
  4. fxml attributes for styling.

我的建议是:


  1. 使用css样式表几乎可用于所有样式。

  2. 使用Java API调用,其中有数千个节点,或者您个人更喜欢API提供的类型安全性和简易调试功能。 / li>
  3. 仅在需要动态更改使用CSS设置的样式时才使用setStyle,并且无法使用设置API 或样式表。

  4. 不要通过fxml应用样式。

  1. Use css stylesheets for almost all of your styling.
  2. Use Java API calls where you have thousands of Nodes or where you personally prefer the type safety and easy debugging capabilities the API offers.
  3. Use setStyle only when you need to dynamically change a style which can only be set using CSS and cannot be set using an API or a stylesheet.
  4. Don't apply styling via fxml.

你的优点用于造型的单个css样式表是:

The advantages of using css stylesheets for styling are:


  1. 您可以将样式与代码分开。

  2. 您可以轻松定义多个样式表并在运行时更改它们以使应用程序具有不同的外观。

  3. 您正在使用专门为样式设计的语言。

  4. 您可以在 SceneBuilder 。

  1. You can separate style from code.
  2. You can easily define multiple stylesheets and change them in and out at runtime to get different looks to your application.
  3. You are making use of a language specifically designed for styling.
  4. You can apply the stylesheets in a design tool like SceneBuilder.

真的,如果你使用的是视觉设计工具,最后一点是至关重要的 - 你真的需要使用css样式表来获取最大的一点。如果您在代码中而不是在css样式表中应用样式,那么当您尝试在可视化设计工具中设计应用程序时,您将无法真实地看到应用程序的外观。如果你通过fxml属性应用样式,那么你的fxml变得难以处理,因为它定义了应用程序的结构和布局以及它的样式 - 通常最好将这些问题分开。

Really, the last point is critical if you are using a visual design tool - you really need to use css stylesheets to get the most out of it. If you apply styles in code rather than in css stylesheets, then you won't really be able to visualize what the application looks like when you are trying to design it in the visual design tool. If you apply style through fxml attributes, then your fxml becomes unwieldy as it is then defining both the structure and layout of your application as well as it's style - and generally it is best to seperate those concerns.

我有时在代码中使用setStyle调用,但主要是因为懒惰而不是因为它是推荐的方法。

I do use setStyle calls in my code sometimes, but mostly that is just because of laziness rather than because it is a recommended way to do it.

使用setStyle动态更改样式的需要应该是非常有限的情况。例如,在JavaFX 2.2中,您无法通过API设置区域的背景颜色。您可以使用styleclass设置它。但是如果你想动态地将背景颜色设置为用户选择的颜色,那么你可以使用类似这样的setStyle方法 button.setStyle( - fx-text-fill:+ userColor.toString() +;); 。所以,非常专业。

The need to dynamically change styles using setStyle should be very limited case. For example, in JavaFX 2.2 you cannot set the background color of a region via API. You can set it using a styleclass. But if you wanted to dynamically set the background color to a user selected color, then you might use a setStyle method like this button.setStyle("-fx-text-fill: " + userColor.toString() + ";");. So, really pretty specialized.

另请注意,在JavaFX 8中,一些只能通过css在JavaFX 2.2中完成的事情(例如设置区域背景)将能够通过Java API在代码中完成。对于上面给出的为区域背景设置用户填充颜色的示例,如果使用JavaFX 8,则使用Java API将是首选方法。

Also note that in JavaFX 8, some of the things which can only be done in JavaFX 2.2 by css (such as setting a region background) will be able to be done in code via Java API. For the example given above of setting a user fill color for a Region background, using the Java API for this would be the preferred method if JavaFX 8 is being used.

In各种使用案例的性能影响条款。在大多数情况下,我的理解(来自论坛中JavaFX css实现维护者的评论)是从大多数性能到最低性能的性能顺序如下所示:

In terms of performance impact of the various usage cases. My understanding (from comments by the JavaFX css implementation maintainer in forums) in most cases is that the performance order from most performant to least performant would be something like below:


  1. 直接API调用。

  2. 样式通过FXML。

  3. 样式通过styleclass应用。

  4. setStyle调用。

  1. Direct API call.
  2. Style via FXML.
  3. Style applied via styleclass.
  4. setStyle call.

请注意,JavaFX中的css应用程序设计得非常高效,因此在许多情况下性能不是主要考虑因素我的猜测是,只有当您设置了数千个节点时,使用css才会对性能产生重大影响,尽管我没有硬数据支持这种猜测。

Note that css application in JavaFX is designed to be quite efficient, so in many cases performance isn't a major consideration. My guess is that it is only when you have thousands of nodes being styled that there would be a major performance impact of using css, though I have no hard data to back that guess up.

过去我看过JavaFX程序员的一些评论,他们不想使用css,而是喜欢使用JavaFX API来设计他们的应用程序。给出的原因包括更容易调试,使用单一语言编写所有内容,更好地与IDE集成以获取方法名称的帮助,编辑和编译时静态类型检查等。如果您喜欢使用Java API而不是css进行样式设置出于这些原因,在你自己的代码中,这是好的。但是,您只能使用Java API完成所需的所有样式。

In the past I have seen some comments by JavaFX programmers that they would like not use css and prefer to use a JavaFX API for styling their application. Reasons given are for things like easier debugging, everything in a single language, better integration with IDEs for assistance on method names, edit and compile time static type checking, etc. And if it is your preference to style using a Java API rather than css for these reasons in your own code, this is fine as far as it goes. But you simply won't be able to accomplish all of the styling you need using only the Java API.

使用CSS和JavaFX中如何使用它的知识对于熟练掌握JavaFX技术至关重要。主要原因是JavaFX中的所有核心控件都由css设计。即使是最琐碎的JavaFX应用程序通常也会使用这些控件,如果您希望能够自定义它们的外观,那么您将需要了解css(即使它只是为了阅读标准的JavaFX css样式表,以便您知道如何标准控件的风格)。当涉及到自定义标准控件时,通常会更容易应用css自定义样式表。

Use of css and knowledge of how to use it in JavaFX is critical to being proficient with JavaFX technology. The main reason for this is that all core controls in JavaFX are styled by css. Even the most trivial JavaFX application usually makes use of these controls and if you want to be able to customize their look, you are going to need to understand css (even if it's just to read the standard JavaFX css stylesheet so that you know how the standard controls are styled). And when it comes to customizing the standard controls, it's generally going to be easier to apply a css custom stylesheet.

这篇关于在JavaFX中,我应该使用CSS还是setter方法来更改UI节点上的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆