如何使用css为JavaFX ContextMenu设置样式?

15

我正试图使用一个单独的CSS文件来改变ContextMenu菜单项的样式。我查看了caspian.css部分,并找到了以下定义:

  • .context-menu
  • .context-menu .separator
  • .context-menu .scroll-arrow
  • .context-menu .scroll-arrow:hover
  • .context-menu:show-mnemonics .mnemonic-underline

我将它们完全复制到我的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部分的可样式化部分中,上下文菜单列出了CSSBridge[context-menu],这似乎很奇怪。像Label之类的其他东西都有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的反馈意见 - jewelsea
会在这个周末完成。谢谢提供链接。 - Jacob Schoen
2个回答

18

这是一个通过CSS为JavaFX上下文菜单进行样式设置的简单示例。

在WinXPsp3、Jdk7u6b14ea和JavaFX 2.2b12上进行了测试。

Java应用程序

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class ContextColor extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    Label label = new Label("Right click for options");
    label.setContextMenu(new ContextMenu(new MenuItem("Help"), new MenuItem("Me")));
    Scene scene = new Scene(label);
    scene.getStylesheets().add(ContextColor.class.getResource("contextcolor.css").toExternalForm());
    stage.setScene(scene);
    stage.show();
  }
}

css 样式表

/** contextcolor.css
 *   place in same folder as ContextColor.java
 *   ensure your build system copies this file to the ContextColor.class output directory on build
 */
.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;
}

我无法确定您的CSS样式未按预期运行的确切原因。一些可能的原因包括:

  1. 您没有正确加载它。
  2. 您的CSS文件未复制到输出路径中。
  3. 您的CSS文件有损坏或语法错误。
  4. 您正在使用早期版本的JavaFX,该版本难以从css中为上下文菜单设置样式。

更新

查看您问题中完整的代码,其中CSS文件通过fxml加载,在这里我可以重现您的问题,即上下文菜单未被设置样式。如果我在代码中将样式表设置在场景上而不是在fxml中设置样式表(与我的测试应用程序中一样),那么一切都正常工作。

当通过fxml设置css时,与在场景上设置样式表不同,fxml不会在场景上设置样式表,而是在场景的父根节点上设置样式表。如果在代码中将样式表添加到父级而不是场景中,则代码实现的行为与fxml相同。因此,这不是针对fxml本身的问题,而是JavaFX 2.2 css处理继承规则中的一个问题。在我看来,css处理是错误的-样式应该是一样的,无论样式表是在场景上还是在场景的根节点上设置的。

我建议在http://javafx-jira.kenai.com对JavaFX运行时控件提出错误报告,附上您的测试用例和指向此StackOverflow问题的链接,JavaFX团队将及时解决该问题。

作为一种解决方法,现在只需在代码中将样式表设置在场景上。


更新

此问题的根本原因似乎是RT-19435:弹出控件未受父级样式表声明的影响


我之前使用的是2.1版本,所以更新到了2.2.0-beta版本。我运行了你的示例代码,它可以正常工作。由于我正在使用FXML文件,所以我将你的示例转换成了FXML格式并再次运行。不幸的是,它在那里无法正常工作。如果您不介意查看一下,我会在我的问题中附上FXML文件的内容。感谢您的帮助。 - Jacob Schoen
感谢您发布jschoen的代码,我根据您发布的代码更新了我的答案。 - jewelsea
我按照建议创建了一个新问题#RT-22871。感谢您的帮助。 - Jacob Schoen
谢谢,这对我也是一个问题。 - Michael Scott

3
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;

/**
 *
 * @author nidhi.a.agrawal
 */
public class Context extends Application {

    @Override
    public void start(Stage stage) throws Exception {
      //  Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

        TextArea notification = new TextArea();
        ContextMenu contextMenu = new ContextMenu();

        Node itemIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_createnew.png")));
        Node con_test_hierIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_Configure_Test _Hierachy.png")));
        Node cutIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_cut.png")));
        Node copyIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_copy.png")));
        Node pasteIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_paste.png")));
        Node insertIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_insert.png")));
        Node editIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_edit.png")));
        Node renameIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_rename.png")));
        Node deleteIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_delete.png")));
        Node tagIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_tag.png")));
        Node refreshIcon = new ImageView(new Image(getClass().getResourceAsStream("icon_refresh.png")));


        MenuItem sap_new = new MenuItem("Create New Sap System", itemIcon);
        MenuItem con_test_hier = new MenuItem("Configure Test Hierarchy", con_test_hierIcon);
        MenuItem cut = new MenuItem("Cut", cutIcon);
        MenuItem copy = new MenuItem("Copy", copyIcon);
        MenuItem paste = new MenuItem("Paste", pasteIcon);
        MenuItem insert = new MenuItem("Insert", insertIcon);
        MenuItem edit = new MenuItem("Edit", editIcon);
        MenuItem rename = new MenuItem("Rename", renameIcon);
        MenuItem delete = new MenuItem("Delete", deleteIcon);
        MenuItem tag = new MenuItem("Tag", tagIcon);
        MenuItem refresh = new MenuItem("Refresh", refreshIcon);

         contextMenu.getItems().addAll(sap_new,con_test_hier,cut,copy,paste,insert,edit,rename,delete,tag,refresh);
notification.setContextMenu(contextMenu);
 Group root = new Group();
        root.getChildren().add(notification);
         Scene scene = new Scene(root);
          scene.getStylesheets().add(Context.class.getResource("contextcolor.css").toExternalForm());
        stage.setScene(scene);
        stage.show();

       }
public static void main(String[] args) {
        launch(args);
    }
}

对于这些菜单,我已经设置了以下样式:---

.

root {
  -fx-background-color: cornsilk; 
  -fx-padding: 10;
}

.context-menu {
  -fx-background-color: #C8CFD7;
  -fx-border-color: #BBC2CC;
  -fx-text-fill: white;
}


.menu-item .label {
  -fx-fill:red;
    -fx-font-family:tahoma;
    -fx-font-size: 12px;;
    -fx-stroke:#C8CFD7;
    -fx-strok-width:.25;
}

.menu-item:focused .label {
  -fx-text-fill: white;
}
.menu-item{
    -fx-border-color: #DDDEE2 transparent #B9C0C8 transparent;

    -fx-border-width:0.50px;

}
.menu-item:focused {
     -fx-background: -fx-accent;
     -fx-background-color: #7092BE;
     -fx-text-fill: -fx-selection-bar-text;
}

它运行良好,我能够更改背景色,边框颜色,设置图标等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接