java 添加样式后如何重置回默认css?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30759310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset back to default css after adding style?
提问by Jonatan Stenbacka
Basically I have changed the css for a text field in javafx by adding a style class like this:
基本上,我通过添加这样的样式类来更改 javafx 中文本字段的 css:
textfield.getStyleClass().add("textfieldstyle");
But then I want to be able to revert it back to its original appearance. But since the original appearance in this case is the default skin for JavaFX, I can't find the original layout for the textfields. I found the textfieldskin properties here, but its a jungle, and I can't find anything about the color of the -fx-control-inner-background, -fx-text-box-border and -fx-focus-color, which is what I want to know.
但后来我希望能够将它恢复到原来的样子。但是由于这种情况下的原始外观是 JavaFX 的默认外观,因此我找不到文本字段的原始布局。我在这里找到了 textfieldskin 属性,但它是一个丛林,我找不到关于 -fx-control-inner-background、-fx-text-box-border 和 -fx-focus-color 的颜色的任何信息,其中是我想知道的。
I've triedtextfield.getStyleClass().remove("textfieldstyle");
and think that does remove the new css, but it doesn't apply the old one again.
我试过textfield.getStyleClass().remove("textfieldstyle");
并认为这确实删除了新的 css,但它不会再次应用旧的。
采纳答案by Jonatan Stenbacka
Thanks to the comments by @UlukBiy and @varren I solved the issue. System.out.println(textfield.getStyleClass());
was of great use since it allowed me to check which style classes were applied on the text field as default. And as it is pointed out in the comments those where text-input
and text-field
.
感谢@UlukBiy 和@varren 的评论,我解决了这个问题。System.out.println(textfield.getStyleClass());
非常有用,因为它允许我检查默认情况下在文本字段上应用了哪些样式类。正如评论中指出的那些 wheretext-input
和text-field
。
So to restore the text field's css to its default value I just did:
因此,要将文本字段的 css 恢复为其默认值,我刚刚做了:
textfield.getStyleClass().clear();
textfield.getStyleClass().addAll("text-field", "text-input");
回答by John C
To reset an element's default styling after setting it using .setStyle("css settings.....");
you can simply use -
要在使用设置后重置元素的默认样式,.setStyle("css settings.....");
您可以简单地使用 -
textfield.setStyle(null);
I'm not sure if this would work on it's own for an element that's had a class applied using .getStyleClass().add("textfieldstyle");
but if not you could do something like -
我不确定这是否适用于应用了类的元素本身,.getStyleClass().add("textfieldstyle");
但如果不是,您可以执行以下操作 -
textfield.getStyleClass().clear();
textfield.setStyle(null);
回答by Eddie Fann
The following test code works for me when adding and removing a class from a control such as Textfield:
在诸如 Textfield 之类的控件中添加和删除类时,以下测试代码对我有用:
import javafx.scene.control.TextInputControl;
public class test
{
protected void setEditable(final TextInputControl toControl, final boolean tlEditable)
{
toControl.setEditable(tlEditable);
if (tlEditable)
{
if (toControl.getStyleClass().contains("non-editable-class"))
{
toControl.getStyleClass().removeAll("non-editable-class");
}
}
else if (!toControl.getStyleClass().contains("non-editable-class"))
{
toControl.getStyleClass().add("non-editable-class");
}
}
}
回答by JMD
A short way to effictively remove your style class even in case of duplicates, with a lambda :
即使在重复的情况下,也可以使用 lambda 有效删除样式类的一种简短方法:
textfield.getStyleClass().removeIf(style -> style.equals("textfieldstyle"));