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

时间:2015-02-05 08:57:58

标签: css colors javafx dialog title

从昨天起,我正在尝试更改我的对话框标题的颜色(或者我的应用程序的所有标题颜色)以获得黑色或深色,因为它是白色并且不能用灰色背景读取。

我仍然认为它应该适用于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);
    }       
}
}

Dialog 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();

正如你所看到的,我试图改变标题图标(注释掉),这很有效。但没有机会改变标题的颜色。

如果我尝试google whith例如javafx,我可以看到许多带有黑色标题颜色的图像。所以必须有可能改变颜色,但我不知道如何。

有什么建议吗?

问候, 汤姆

1 个答案:

答案 0 :(得分:2)

我有待纠正,但我不认为你可以设置NATIVE标题栏的颜色。我认为你在谷歌搜索结果中看到的是自定义(用户)标题栏。

也许此链接对您有用:https://arnaudnouard.wordpress.com/2013/02/02/undecorator-add-a-better-look-to-your-javafx-stages-part-i/

您还可以查看FXControls Dialogs源代码,看看他们是如何做到的:http://fxexperience.com/controlsfx/features/dialogs/