簡體   English   中英

MenuItem Focused Label JavaFX上的CSS樣式

[英]CSS styling on MenuItem Focused Label JavaFX

我遇到一些問題,無法正常工作。 所以我有一個自定義上下文菜單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或代碼設置焦點標簽的文本填充屬性? 我什至不知道是否有可能,但是在正確方向上的任何指針將不勝感激。

只要將錯誤設置為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 <-只需加載CSS和FXML

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上下載此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM