- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我有一个带有这段代码的简单场景:
scene.getStylesheets().add("packagename/testcss.css");
我的 testcss.css 是:
.button {
-fx-background-color: #DDFFA4;
}
.button:hover {
-fx-background-color: #9ACD32;
}
我实现的是在 Web 应用程序中的悬停效果。
...
问题
我如何在没有单独的 css 文件的情况下,仅通过我的按钮的 setStyle 方法实现相同的效果?
问题是 setStyle 只接受样式定义而忽略了选择器。
button.setStyle("-fx-background-color: #DDFFA4;");
选择器,在我的悬停情况下是伪类:
.button:hover
但是我应该在哪里设置呢?
此外,javadoc 明确地从 setStyle 方法参数中排除了选择器:
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 代码中进行样式操作的公共(public) API 的一部分公开。
如果您想在不使用样式表的情况下从 Java 代码更改样式属性,则需要根据事件或更改监听器设置样式。下面的示例中有几个选项。
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 样式表,我只会在代码而不是样式表中真正做到这一点。
在 JavaFX 8 中有一个公共(public) API,它允许您(如果您愿意)manipulate Region background colors without use of CSS .
示例程序输出(悬停更新按钮):
关于css - javafx 2 和 css 伪类 : setting hover attributes in setStyle method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13074459/
我正在尝试移动小部件中的 qx.html.Element。所以我创建了一个监听器来捕捉鼠标按下和一个监听器来捕捉鼠标移动等等。qooxdoo 中的代码 playground 但是当我尝试例如 setS
代码:我在舞台上添加一个按钮: Button btn = new Button("Change size"); 然后设置当前文本颜色: btn.setStyle("-fx-text-fill:red"
我正在使用 QGraphicsProxyWidget 将 QWidget 嵌入到 QGraphicsScene 中。问题在于绘制这些小部件的样式。来自documentation : A top-lev
为了在 JavaFX 中为许多元素设置背景样式,您需要使用 css 文件,或使用 .setStyle。 在我的例子中,我必须从 ComboBox 设置下拉菜单的背景样式,但我必须使用 .setStyl
我有以下代码片段: YUI().use('event', function (Y) { var button = Y.one("#ckbox"); button.ancestor(".
我正在创建一个 JavaFX 应用程序,但我在更改某些组件的背景颜色时遇到了问题。对于按钮,我可以更改它们的背景半径,但不能更改它们的背景颜色。对于 TableView,我也无法更改背景颜色。 这是我
我有一个实现自定义 ProgressBar 的对象。有时我希望小节的轨道为白色,有时我希望它为黑色。我发现可以使用以下命令在我的 .css 文件中为其设置默认值: .my-bar .track {
我有一个对象,我用 CSS 对该对象进行样式化。在我的程序中,我想更改样式中的特定元素,例如背景颜色。我遇到的问题是,当我调用 setStyle(-fx-background-color:red) 时
有一个选项可以绑定(bind) CSS 文件以向 JavaFX 组件添加样式。 但我想在代码中动态更改一些属性。 有一个 setStyle() 方法,但没有足够的文档来使用它。 我想从 setStyl
我在 UIToolBar 中有一个 UIBarButtonItem。我已经设置了这样的代码: UIBarButtonItem *barbutton=[[UIBarButtonItem alloc]in
如何使用 YUI3 的设置样式来设置类似 -webkit-border-radius: 10px; 的东西? 例子: Y.one('#mydiv').setStyle('-webkit-border-
http://i44.tinypic.com/b69h1h.jpg 看看上图,尤其是 Firebug 窗口。 现在看看这里: http://i40.tinypic.com/v2z4up.jpg 有没有
我正在尝试将 setStyle 设置为扩展 dialogFragment 的自定义对话框,构造函数看起来像: MyCustomDialog() { super(); setStyle(STYL
我正在使用 Mapbox-GL 构建 map 网络应用程序。它有很多很酷的功能。我已经按照 Mapbox website 上的示例设置了切换 basemap (即卫星、地形等)的按钮。 . 我遇到的问
在一个组件中,我试图定位一个 dom 节点并更改它的样式,但这不起作用,谁能告诉我我在这里做错了什么? @Input() progress:number = 0; ... ngOnChanges()
我试图在调整页面大小时(或在较小的显示器上打开)时动态调整页面上的某些元素。有一段代码大概是这样的: $('some_id').setStyle({'margin-left':'200px'}); 它
我有一个 MooTools 脚本,它处理带按钮的面板(切换面板内容)。 这是脚本 - 每个按钮都设置相应的面板可见 + 更改其自身的背景: buttons.addEvent('mouseenter',
下面的代码创建了一个QComboBox,并分配了QAbstractTableModel模型。奇怪的是,如果 app.setStyle("cleanlooks") 被注释掉,QCombo 在单击时不会下
我正在尝试使用 setStyle() 方法为我的 PyQt5 应用程序设置自定义样式,但它不起作用。 我也不完全确定应该把它放在哪里。 在我的主要我尝试做: if __name__ == '__mai
有什么方法可以将样式重置为我的 CSS 中的样式吗? 例子: #foo {width: 50px; height: 50px; position: absolute; top: 150px; left
我是一名优秀的程序员,十分优秀!