简体   繁体   English

JavaFX从文档控制器更改全局样式表

[英]JavaFX Changing the Global Stylesheet from Document Controller

I'm new to JavaFX and I'm currently working on a JavaFXML application. 我是JavaFX的新手,目前正在开发JavaFXML应用程序。 What I'm trying to do is find a way to change the global Stylesheet when I click on a button. 我想做的是找到一种方法,当我单击按钮时更改全局样式表。 My current code is this.. 我当前的代码是这个。

Main.Java Main.Java

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;

    public class CssChange extends Application {

    static FXMLDocumentController myControllerHandle;

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

    myControllerHandle = (FXMLDocumentController)loader.getController();

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

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

Controller.java Controller.java

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.Pane;

    public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

@FXML
private Button button;

@FXML
private void CssChange(ActionEvent event) throws IOException{
    Parent root;
    root = (AnchorPane) FXMLLoader.load(getClass().getResource("CssChange.fxml"));
    System.out.println("You clicked me!");
    label.setText("Hello World!");
    String css = CssChange.class.getResource("login2.css").toExternalForm();
    root.getStylesheets().clear();
    root.getStylesheets().add(css);
    root.applyCss();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

FXML Document FXML文件

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

    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.text.Font?>

    <AnchorPane  prefHeight="800.0" prefWidth="480.0" 
       stylesheets="@Login.css" xmlns="http://javafx.com/javafx/8.0.111" 
       xmlns:fx="http://javafx.com/fxml/1" 
       fx:controller="csschange.FXMLDocumentController" fx:id="root">

<children>
  <Button layoutX="14.0" layoutY="25.0" mnemonicParsing="false" 
       onAction="#CssChange" text="CSS Change" fx:id="button">
     <font>
        <Font name="Arial Bold Italic" size="18.0" />
     </font>
  </Button>
  <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" 
       fx:id="label" />
  </children>
  </AnchorPane >

First CSS 第一个CSS

    .root {
    -fx-background-color:
    linear-gradient(#000000 0%, #ffffff 100%);
    }

    .label {
    -fx-font-size: 12px;
    -fx-font-weight: bold;
    -fx-text-fill: #333333;
    -fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
    }

Second CSS 第二个CSS

    .root {
    -fx-background-image: url("background.jpg");
    }

    .label {
    -fx-font-size: 12px;
    -fx-font-weight: bold;
    -fx-text-fill: #ffffff;
    -fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
    }

The code seems to work okay and when I click on the button I don't get any errors but, nothing happens. 代码似乎可以正常工作,当我单击按钮时,我没有收到任何错误,但是什么也没有发生。 The second CSS does not seem to be called at all. 第二个CSS似乎根本没有被调用。 I'd really appreciate any help I can get. 我会很感激我能得到的任何帮助。 Thank you in advance. 先感谢您。

Have you tried to hide then show the stage (or your root pane)? 您是否尝试过隐藏然后显示阶段(或您的根窗格)? Maybe actualize the display would change something... 也许实现显示会有所改变...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM