0

我有个问题。我想从另一个控制器类更改窗格的颜色。我正在使用这段代码:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Menu.fxml"));
    try {
        Parent loaded = (Parent) loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    MenuController controller = (MenuController) loader.getController();
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Pane pane = controller.getRedPane();
            pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
        }
    });

加载的、控制器和窗格不为空。但是窗格的颜色没有改变,有人可以帮我解决这个问题吗?非常感谢。

[编辑]

public class MenuController implements Initializable
{
    @FXML
    private GridPane MenuRoot;

    @FXML
    private Pane redPane;


    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

    }

   @FXML
   private void changeGridSize(ActionEvent event){
       new ChangeSizes();
   }

    public GridPane getMenuRoot(){
        return this.MenuRoot;
    }

    public Pane getRedPane(){
        return this.redPane;
    }
}

[编辑]

<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308"      maxWidth="1.7976931348623157E308" 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="nl.voxworks.homeserver.client.MenuController">
  <columnConstraints>
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    </rowConstraints>
   <children>
      <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" />
      <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
   </children>
</GridPane>

[编辑](包括不同项目的所有文件)包javafxapplication16;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color;

public class ChangeSize {

public ChangeSize(){
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
    try {
        loader.load();
    } catch (IOException ex) {
        Logger.getLogger(ChangeSize.class.getName()).log(Level.SEVERE, null, ex);
    }
    FXMLDocumentController controller = (FXMLDocumentController) loader.getController();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
             Pane pane = controller.getPane();
            pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
        }
    });
    }
}

FXML 文档控制器。

package javafxapplication16;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;

public class FXMLDocumentController implements Initializable {

@FXML
private Pane redPane;

@FXML
private void changeGridSize(ActionEvent event){
    new ChangeSize();
}


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

}    

public Pane getPane(){
    return this.redPane;
}
}

JavaFXApplication16

package javafxapplication16;

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

public class JavaFXApplication16 extends Application {

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

    Parent loaded=null;
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
    try {
        loaded = (Parent) loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scene scene = new Scene(loaded);

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

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}
}

FXML

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

<GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" 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="javafxapplication16.FXMLDocumentController">
  <columnConstraints>
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  </columnConstraints>
   <rowConstraints>
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    </rowConstraints>
    <children>
      <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" />
      <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Button mnemonicParsing="false" onAction="#changeGridSize" text="Button" />
   </children>
</GridPane>
4

1 回答 1

1

这是我的代码:希望对您有所帮助

FXMLDocumentController.class

public class FXMLDocumentController implements Initializable {

    @FXML
    private Pane redPane;

    @FXML
   private void changeGridSize(ActionEvent event){
      // new ChangeSizes();
   }


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


    }    

    public Pane getPane(){
        return this.redPane;
    }

}

JavaFXMLApplication3.class

public class JavaFXMLApplication3 extends Application {

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

        Parent loaded=null;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
    try {
        loaded = (Parent) loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scene scene = new Scene(loaded);

        stage.setScene(scene);
        stage.show();
    FXMLDocumentController controller = (FXMLDocumentController) loader.getController();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Pane pane = controller.getPane();
            pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
        }
    });


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

FXML文档.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.*?>

<GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308"      maxWidth="1.7976931348623157E308" 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="javafxmlapplication3.FXMLDocumentController">
  <columnConstraints>
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" />
    </rowConstraints>
   <children>
      <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" />
      <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
   </children>
</GridPane>

我测试了您的代码,问题是您创建了另一个未显示的窗格 redPane:当您调用 FXMLLoader 时,您创建了一个未在阶段显示的新加载程序。

我建议你一个可能的解决方案:创建一个静态舞台......示例代码:

public class JavaFXMLApplication3 extends Application {

  static Stage staticstage;

      //  Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
       // FXMLLoader loader

   @Override
public void start(Stage stage) throws Exception {
  //  Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
   // FXMLLoader loader
   staticstage=stage;
    Parent loaded=null;
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
    try {
        loaded = (Parent) loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scene scene = new Scene(loaded);

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



    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

并更改类 ChangeSize

public class ChangeSize {

    public ChangeSize() {

        Parent loaded=null;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
        try {
            loaded = (Parent) loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(loaded);

        JavaFXMLApplication3.staticstage.setScene(scene);
        JavaFXMLApplication3.staticstage.show();  
        FXMLDocumentController controller = (FXMLDocumentController) loader.getController();
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    Pane pane = controller.getPane();
                    pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));

                }
            });



        }


    }

另一种解决方案是将 FXMLDocumentController 传递给 ChangeSize 类...示例代码:

public class ChangeSize {

public ChangeSize(FXMLDocumentController controller) {

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Pane pane = controller.getPane();
                pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));

            }
        });



    }

并在 FXMLDocumentController 类中更改 changeGridSize

   @FXML
   private void changeGridSize(ActionEvent event){
       new ChangeSize(this);
   }
于 2014-12-31T20:04:53.950 回答