javafx 2和css伪类:在setStyle方法中设置hover属性 [英] javafx 2 and css pseudo-classes: setting hover attributes in setStyle method

查看:1207
本文介绍了javafx 2和css伪类:在setStyle方法中设置hover属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的场景使用这段代码:

I have a simple Scene with this code:

scene.getStylesheets().add("packagename/testcss.css");

我的testcss.css是:

And my testcss.css is:

.button { 
    -fx-background-color: #DDFFA4;
}

.button:hover {
    -fx-background-color: #9ACD32;
}

我实现的是在web应用程序中aka hover的漂亮效果。

What i achieve is the nice effect that is aka hover when in web applications.

...

QUESTION

如何通过我的Button的setStyle方法实现相同的效果,但没有单独的css文件?

问题是setStyle只接受样式定义并忽略选择器。

QUESTION
How can i achieve the same effect but without a separate css file, just via the setStyle method of my Button?
The problem is that setStyle accepts just the style definition and leave out the selector.

button.setStyle("-fx-background-color: #DDFFA4;");

选择器,在我的悬浮情况下是伪类:

The selector, in my hover case is the pseudo-class:

.button:hover

我应该设置它吗?

but where am I supposed to set it?

此外,javadoc是从setStyle方法参数中排除选择器的显式方法:

Moreover, the javadoc is explicit in excluding the selector from the setStyle method argument:


请注意,像HTML样式属性一样,此变量包含样式
的属性和值,而不是样式规则的选择器部分。

Note that, like the HTML style attribute, this variable contains style properties and values and not the selector portion of a style rule.


推荐答案

正如你在问题中所注意到的,从JavaFX 2.2开始,css伪类不会暴露您可以在Java代码中使用的样式操作的公共API。

As you note in your question, as of JavaFX 2.2, the css pseudoclasses aren't exposed as part of the public API you can use for style manipulation inside Java code.

如果要在不使用样式表的情况下从Java代码更改样式属性,您需要设置样式事件或更改列表。在下面的示例中有几个选项。

If you want to change style attributes from Java code without using a stylesheet, you need to set the styles based on events or changelisteners. There are a couple of options in the sample below.

import javafx.application.Application;
import javafx.beans.binding.*;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;

/** Changing button styles on hover without a css stylesheet. */
public class ButtonBackgroundChanger extends Application {
  private static final String STANDARD_BUTTON_STYLE = "-fx-background-color: #DDFFA4;";
  private static final String HOVERED_BUTTON_STYLE  = "-fx-background-color: #9ACD32;";

  public static void main(String[] args) throws Exception { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    Button configure = new Button("Configure");
    changeBackgroundOnHoverUsingBinding(configure);
    Button update = new Button("Update");
    changeBackgroundOnHoverUsingEvents(update);

    VBox layout = new VBox(10);
    layout.setAlignment(Pos.CENTER);
    layout.setStyle("-fx-padding: 10;");
    layout.getChildren().addAll(configure, update);
    stage.setScene(new Scene(layout));
    stage.show();
  }

  private void changeBackgroundOnHoverUsingBinding(Node node) {
    node.styleProperty().bind(
      Bindings
        .when(node.hoverProperty())
          .then(
            new SimpleStringProperty(HOVERED_BUTTON_STYLE)
          )
          .otherwise(
            new SimpleStringProperty(STANDARD_BUTTON_STYLE)
          )
    );
  }

  public void changeBackgroundOnHoverUsingEvents(final Node node) {
    node.setStyle(STANDARD_BUTTON_STYLE);
    node.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        node.setStyle(HOVERED_BUTTON_STYLE);
      }
    });
    node.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        node.setStyle(STANDARD_BUTTON_STYLE);
      }
    });
  }    
}

我真的只能在代码中如果颜色需要真正动态的使用样式表与预定义的颜色和样式有问题,或者如果有一些其他厌恶使用CSS样式表,样式表。

I'd only really do it in code rather than a stylesheet if colors needed to be really dynamic making use of stylesheets with predefined colors and styles problematic, or if there was some other aversion to using css stylesheets.

在JavaFX 8中有一个公共API,允许你(如果你想)操纵区域背景颜色而不使用CSS

In JavaFX 8 there is be a public API which allows you (if you want to) to manipulate Region background colors without use of CSS.

示例程序输出(更新按钮悬停):

Sample program output (with the Update button hovered):

这篇关于javafx 2和css伪类:在setStyle方法中设置hover属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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