JavaFX如何更改对话框/窗口标题的颜色?

3
自昨天以来,我一直试图更改对话框标题(或可能是应用程序所有标题颜色)的颜色,以获得黑色或深色颜色,因为它是白色,无法在灰色背景下阅读。
我仍然认为它应该使用css工作,但我无法找到标题颜色的具体条目。
我在dialog.css中尝试了这样的内容,但没有成功,所以注释掉了:
/*
.root {     
-fx-text-base-color: blue;
-fx-text-background-color: green;
-fx-text-inner-color: red;
-fx-selection-bar-text: yellow;
}
*/

这是我的Dialog类:

package de.test.dialog;

import java.io.IOException;

import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;


public class Dialog extends Stage {

public static final String OK_DIALOG = "OK";

private final String OK_XML = "/fxml/dialog_ok.fxml";


public enum DIALOG_ACTION {
    BUTTON_1, BUTTON_2, BUTTON_3, NOTHING, CLOSE_WINDOW
}

private DialogController controller = null;
private String message = null;


public Dialog(String name, String ... buttonName) {     
    String resource = getFXMLResource(name);
    if (resource != null) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(resource));
            Parent root = (Parent) fxmlLoader.load();
            controller = fxmlLoader.getController();
            controller.setButtons(buttonName);
            setScene(new Scene(root));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent event) {
            System.out.println("Closing?");             
        }
    });
}

private String getFXMLResource(String name) {
    String fxmlResource = null;

    switch(name) {
        case OK_DIALOG:
            fxmlResource = OK_XML;
            break;
        default:
            break;
    }

    return fxmlResource;
}

public Dialog.DIALOG_ACTION getAction() {
    if (controller != null) {
        return controller.getAction();
    }
    else {
        return DIALOG_ACTION.NOTHING;
    }
}

public void setMessage(String sMessage) {
    this.message = sMessage;
    if (controller != null) {
        controller.setMessage(message);
    }
}

public void setIcon(Image image) {      
    if (controller != null) {
        controller.setIcon(image);
    }       
}
}

对话框 fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="176.0" prefWidth="400.0" stylesheets="@styles/dialog.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.test.dialog.DialogController">
   <children>
  <ImageView fx:id="imgIcon" fitHeight="48.0" fitWidth="48.0" layoutX="8.0" layoutY="25.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="25.0">
     <image>
        <Image url="@../icons/dialog/48a.png" />
     </image>
  </ImageView>
  <Text fx:id="txtMessage" layoutX="85.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="TEST" wrappingWidth="300.00" AnchorPane.leftAnchor="94.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="25.0">
     <font>
        <Font size="14.0" />
     </font>
  </Text>
  <Button fx:id="btn1" defaultButton="true" layoutX="295.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="OK" AnchorPane.rightAnchor="15.0">
     <font>
        <Font size="14.0" />
     </font>
  </Button>
  <Button fx:id="btn2" cancelButton="true" layoutX="180.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="Abbrechen" visible="false" AnchorPane.rightAnchor="120.0">
     <font>
        <Font size="14.0" />
     </font>
  </Button>
  <Button fx:id="btn3" layoutX="102.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="Button 3" visible="false" AnchorPane.rightAnchor="225.0">
     <font>
        <Font size="14.0" />
     </font>
  </Button>
   </children>
</AnchorPane>

调用我的对话框:

    Dialog dialog = new Dialog(Dialog.OK_DIALOG, "Löschen", "Abbrechen");
    dialog.initModality(Modality.APPLICATION_MODAL);
    dialog.initOwner(((Node)e.getSource()).getScene().getWindow());
    dialog.setResizable(false);
    dialog.setTitle("Dateianhang löschen");     
    dialog.setMessage("Wollen Sie die ausgewählte(n) Datei(en) wirklich löschen?");

    // Get the Stage.
    //Stage stage = (Stage) dialog.getScene().getWindow();
    // Add a custom icon.
    //stage.getIcons().add(new Image("/icons/dialog/48a.png"));

    dialog.showAndWait();

您可以看到,我试图更改标题图标(已注释),并且成功了。但是没有机会更改标题的颜色。

如果我尝试使用例如JavaFX的谷歌搜索,我可以看到许多具有黑色标题颜色的图像。因此,更改颜色肯定是可能的,但我不知道如何操作。

有什么建议吗?

问候, 汤姆


这篇文章帮助我构建了自己的对话框(无装饰/透明,自定义标题栏和鼠标事件以移动): https://dev59.com/6Wgt5IYBdhLWcg3w2A37 - Tom
1个回答

3

看起来你是对的,如果我将我的系统标题颜色(Windows 7)更改为黑色,则JavaFX窗口也使用黑色。 我可以使用StageStyle.UNDECORATED或TRANSPARENT来隐藏标题栏,但没有更改字体颜色的可能性。 - Tom

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