繁体   English   中英

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

[英]JavaFX Changing the Global Stylesheet from Document Controller

我是JavaFX的新手,目前正在开发JavaFXML应用程序。 我想做的是找到一种方法,当我单击按钮时更改全局样式表。 我当前的代码是这个。

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

    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文件

    <?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 >

第一个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 );
    }

第二个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 );
    }

代码似乎可以正常工作,当我单击按钮时,我没有收到任何错误,但是什么也没有发生。 第二个CSS似乎根本没有被调用。 我会很感激我能得到的任何帮助。 先感谢您。

您是否尝试过隐藏然后显示阶段(或您的根窗格)? 也许实现显示会有所改变...

暂无
暂无

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

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