我正在尝试使用单独的CSS文件更改ContextMenu
项目的样式。我看着caspian.css部分,发现如下定义:如何使用css设置JavaFX ContextMenu的样式?
- .context菜单
- .context菜单.separator
- .context菜单.scroll箭头
- .context-菜单.scroll箭头:悬停
- .context菜单:显示-助记符.mnemonic下划线
我复制了那些恰好到我的CSS文件,并更改只是背景颜色值作为测试:
.context-menu {
-fx-skin: "com.sun.javafx.scene.control.skin.ContextMenuSkin";
-fx-background-color: #006699;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 0 6 6 6, 0 5 5 5, 0 4 4 4;
/* -fx-padding: 0.666667em 0.083333em 0.666667em 0.083333em; 8 1 8 1 */
-fx-padding: 0.333333em 0.083333em 0.666667em 0.083333em; /* 4 1 8 1 */
}
.context-menu .separator {
-fx-padding: 0.0em 0.333333em 0.0em 0.333333em; /* 0 4 0 4 */
}
.context-menu .scroll-arrow {
-fx-padding: 0.416667em 0.416667em 0.416667em 0.416667em; /* 5 */
-fx-background-color: #006699;
}
.context-menu .scroll-arrow:hover {
-fx-background: -fx-accent;
-fx-background-color: #006699;
-fx-text-fill: -fx-selection-bar-text;
}
.context-menu:show-mnemonics .mnemonic-underline {
-fx-stroke: -fx-text-fill;
}
这显然不工作,否则我不会在这里。无论我改变什么样的价值观,它似乎都没有影响。
我打开了JavaFX Scene Builder来看看(注意我用这个作为最后的手段,因为我觉得它很笨拙)。我注意到在css部分的Styleable部分下面的列表CSSBridge[context-menu]
的上下文菜单看起来很奇怪。其他东西,如标签有Label[label]
。
有人可以向我解释这里发生了什么,因为它似乎忽略了我的css文件的上下文菜单和使用caspian.css中的默认值?
附加示例FXML文件,css和java代码来运行。
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="myroot" xmlns:fx="http://javafx.com/fxml">
<children>
<Label text="Right click for options">
<contextMenu>
<ContextMenu>
<items>
<MenuItem text="Help" />
<MenuItem text="Me" />
</items>
</ContextMenu>
</contextMenu>
</Label>
</children>
<stylesheets>
<URL value="@contextcolor.css" />
</stylesheets>
</AnchorPane>
contextcolor.css
.root {
-fx-background-color: cornsilk;
-fx-padding: 10;
}
.context-menu {
-fx-background-color: #006699;
-fx-text-fill: white;
}
.menu-item .label {
-fx-text-fill: yellow;
}
.menu-item:focused .label {
-fx-text-fill: white;
}
Test.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
Application.launch(Test.class, args);
}
@Override
public void start(Stage stage) throws Exception {
System.out.println(com.sun.javafx.runtime.VersionInfo.getVersion());
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
}
[上SceneBuilder反馈这里]提供(https://forums.oracle.com/forums/thread .jspa?threadID = 2369765) – jewelsea
这周末会做。感谢您的链接。 –