【问题标题】:CSS styling on MenuItem Focused Label JavaFXMenuItem 焦点标签 JavaFX 上的 CSS 样式
【发布时间】:2014-07-07 14:53:05
【问题描述】:

我在让某些东西工作时遇到了一些问题。所以我有一个自定义上下文菜单validation-error-context和一个相应的css:

#validation-error-context
{
    -fx-background-color: #ffbbbb;  
    -fx-border-color: #f00; 
}

在此上下文中,我还想设置 MenuItems 的样式,因此我将以下 css 应用于它们:

#error-menu-item
{
    -fx-background-color: #ffbbbb;  
}

.error-menu-item:focused
{
    -fx-background-color: #ffbbbb;
    -fx-text-fill: red;
}

一切都按计划进行,项目和上下文具有红色背景和边框。现在的问题是,当我想在 MenuItem 的焦点状态上设置文本填充颜色时,这通常是通过以下方式完成的:

.menu-item:focused .label 
{
  -fx-text-fill: red;
}

但是,这显然会将该样式应用于项目中的所有 menuItems,这不是我想要的,并且使用下面的语句不适用于我正在使用的实例。

.error-menu-item:focused .label 
{
    -fx-text-fill: red;
}

有没有办法通过css或代码设置焦点标签的文本填充属性?我什至不确定它是否可能,但任何指向正确方向的指针将不胜感激。

【问题讨论】:

    标签: java css javafx menuitem


    【解决方案1】:

    只要您将错误设置为 css 类而不是 id,您的代码就可以工作:(#)

    查看您的代码的最小可编译示例:

    MenuController.java

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.MenuButton;
    import javafx.scene.control.MenuItem;
    public class MenuController implements Initializable {
        @FXML private MenuButton OkMenu;
        @FXML private MenuItem foo1;
        @FXML private MenuItem bar2;
        @Override
        public void initialize(URL arg0, ResourceBundle arg1) {
            //Define foo1 and bar 2 as errors:
            foo1.getStyleClass().add("error-menu-item");
            bar2.getStyleClass().add("error-menu-item");
        }
    }
    

    Main.java

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("menu.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
            stage.setScene(scene);
            stage.setTitle("Menu");
            stage.show();
    
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    style.css

    .error-menu-item
    {
        -fx-background-color: #ffbbbb;  
    }
    
    .error-menu-item:focused .label
    {
        -fx-text-fill: red;
    }
    

    menu.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MenuController">
    <children><MenuButton layoutX="0.37109375" layoutY="7.5" mnemonicParsing="false" text="FooMenu">
      <items>
        <MenuItem fx:id="foo1" mnemonicParsing="false" text="Foo1" />
        <MenuItem fx:id="foo2" mnemonicParsing="false" text="Foo2" /><MenuItem fx:id="foo3" mnemonicParsing="false" text="Foo3" />
      </items>
    </MenuButton><MenuButton layoutX="99.7421875" layoutY="7.5" mnemonicParsing="false" text="BarMenu">
      <items>
        <MenuItem fx:id="bar1" mnemonicParsing="false" text="Bar1" />
        <MenuItem fx:id="bar2" mnemonicParsing="false" text="Bar2" /><MenuItem fx:id="bar3" mnemonicParsing="false" text="Bar3" />
      </items>
    </MenuButton>
    </children>
    

    您可以在Gist下载此代码。

    【讨论】:

    • 干杯人,完美运行 - 我使用 setId 来设置 css 而不是 getStyleClass().add()!两个函数之间行为不同的原因是什么?
    • 抱歉所有问题,但是还有没有办法让一个类有多个样式状态?例如,一个文本字段可以有一个正常的 css 状态,然后是一个错误的 css 状态,它将设置焦点突出显示?
    • 首先,如果您setId,您可能只有一个具有该样式的类,因为 Id 应该是特定的(而且它是 fx:id 尝试在 CSS 中添加 #foo2在上面的代码中,你就会明白了。此外,类更通用,所以你可以有一个子类。比如.error-menu-item:focused,而有id,你不能。第二条评论:创建一个交换css类的监听器,当会发生这种变化。(如果您还有更多问题,请再发一个question。不要在 cmets 中扩展这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多