java—设置fxml对象的值和同时实现fxml操作事件的问题

a5g8bdjr  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(185)

考虑到fxml对象的加载和动作事件的创建,我遇到了一个问题:这是我的代码:
我用以下代码打开另一个类中有问题的场景:
控制器1:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {

    private Stage window;
    public Button profilScene;

    public void profilSwitch(ActionEvent event) throws IOException {
        window = (Stage)((Node)event.getSource()).getScene().getWindow();
        Controller2 controllerProfiluebersicht = new Controller2();

        FXMLLoader loaderProfil = new FXMLLoader(getClass().getResource("/sample/profilübersicht.fxml"));
        loaderProfil.setController(controllerProfiluebersicht);

        Parent profilScene = loaderProfil.load();
        controllerProfiluebersicht.label();

        Scene profil = new Scene(profilScene);

        window.setScene(profil);
    }
    }

问题场景:控制器2:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Controller2 {

    FXMLLoader loaderProfil = new FXMLLoader(getClass().getResource("/sample/profilübersicht.fxml"));

    private Stage window;
    //-----------------------------set label ---------------------------------------

    public void label (){

        profilMiId.setText("example 1");
        profilEmail.setText("example 2");
        profilPasswort.setText("example 3");

    }

    @FXML
    private Label profilDatum;

    @FXML
    private Label profilEmail;

    @FXML
    private Label profilMiId;

    @FXML
    private Label profilPasswort;

    //----------------------------------------------------------------------------------

    @FXML
    private Button profilKnopfRaum;
//this button imported from the fxml file needs a action event method

}

fxml控制器1:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="profilScene" layoutX="272.0" layoutY="188.0" mnemonicParsing="false" onAction="#profilSwitch" text="scene 2" />
   </children>
</AnchorPane>

fxml控制器2:

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

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="750.0" maxWidth="1295.0" minHeight="750.0" minWidth="1295.0" prefHeight="750.0" prefWidth="1295.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox prefHeight="750.0" prefWidth="378.0" style="-fx-background-color: #E20074;" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Pane prefHeight="241.0" prefWidth="378.0">
               <children>
                  <Label layoutX="102.0" layoutY="63.0" textFill="WHITE">
                     <font>
                        <Font name="TeleLogo" size="45.0" />
                     </font>
                  </Label>
                  <Label layoutX="55.0" layoutY="128.0" prefHeight="23.0" prefWidth="269.0" textFill="WHITE">
                     <font>
                        <Font name="TeleNeo Office Regular" size="19.0" />
                     </font>
                  </Label>
               </children>
            </Pane>
            <Button fx:id="profilKnopfRaum" mnemonicParsing="false" prefHeight="93.0" prefWidth="416.0" style="-fx-background-color: #ca1f7b;" text="Übersicht der Räume" textFill="WHITE">
               <font>
                  <Font name="Tele-GroteskFet" size="30.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
            <Pane prefHeight="49.0" prefWidth="378.0" />
            <Button fx:id="profilKnopfReservieren" mnemonicParsing="false" prefHeight="93.0" prefWidth="378.0" style="-fx-background-color: #ca1f7b;" text="Raum reservieren" textFill="WHITE">
               <font>
                  <Font name="Tele-GroteskFet" size="30.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
            <Pane prefHeight="49.0" prefWidth="378.0" />
            <Button mnemonicParsing="false" prefHeight="93.0" prefWidth="378.0" style="-fx-background-color: #ca1f7b;" text="Profil" textFill="WHITE">
               <font>
                  <Font name="Tele-GroteskFet" size="30.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
               <effect>
                  <DropShadow height="255.0" radius="63.25" width="0.0" />
               </effect>
            </Button>
         </children>
      </VBox>
      <BorderPane layoutX="378.0" prefHeight="750.0" prefWidth="918.0">
         <top>
            <VBox prefHeight="174.0" prefWidth="927.0" BorderPane.alignment="CENTER">
               <children>
                  <Pane prefHeight="94.0" prefWidth="917.0">
                     <children>
                        <Label fx:id="profilDatum" layoutX="52.0" layoutY="32.0" prefHeight="23.0" prefWidth="105.0" text="01.01.2021">
                           <font>
                              <Font name="TeleNeo Office Regular" size="19.0" />
                           </font>
                        </Label>
                     </children></Pane>
                  <Pane prefHeight="83.0" prefWidth="918.0" style="-fx-background-color: #dbdbdb;">
                     <children>
                        <Label layoutX="424.0" layoutY="22.0" text="Profil">
                           <font>
                              <Font name="TeleNeo Office Bold" size="32.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
               </children>
            </VBox>
         </top>
         <center>
            <Pane prefHeight="560.0" prefWidth="918.0" BorderPane.alignment="CENTER">
               <children>
                  <VBox prefHeight="576.0" prefWidth="918.0">
                     <children>
                        <Pane prefHeight="66.0" prefWidth="918.0" />
                        <Pane prefHeight="35.0" prefWidth="918.0" style="-fx-background-color: #dbdbdb;" />
                        <GridPane prefHeight="481.0" prefWidth="918.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            <RowConstraints maxHeight="155.0" minHeight="10.0" prefHeight="122.0" vgrow="SOMETIMES" />
                            <RowConstraints maxHeight="197.0" minHeight="10.0" prefHeight="197.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                           <children>
                              <Pane prefHeight="200.0" prefWidth="200.0">
                                 <children>
                                    <Label alignment="CENTER_RIGHT" layoutX="175.0" layoutY="51.0" prefHeight="62.0" prefWidth="193.0" text="E-Mail Adresse:">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                 </children>
                              </Pane>
                              <Pane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
                                 <children>
                                    <Label alignment="CENTER_RIGHT" layoutX="176.0" layoutY="7.0" prefHeight="62.0" prefWidth="193.0" text="Mitarbeiter-ID:">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                    <Label alignment="CENTER_RIGHT" layoutX="176.0" layoutY="93.0" prefHeight="62.0" prefWidth="193.0" text="Passwort:">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                 </children>
                              </Pane>
                              <Pane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2">
                                 <children>
                                    <Label alignment="CENTER_RIGHT" layoutX="174.0" layoutY="68.0" prefHeight="62.0" prefWidth="193.0" text="Reservierungen:">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                 </children>
                              </Pane>
                              <Pane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
                                 <children>
                                    <Label fx:id="profilEmail" alignment="CENTER" layoutY="51.0" prefHeight="62.0" prefWidth="302.0" text="Hier ist die E-Mail Adresse">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                 </children>
                              </Pane>
                              <Pane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
                                 <children>
                                    <Label fx:id="profilMiId" alignment="CENTER" layoutY="8.0" prefHeight="62.0" prefWidth="302.0" text="Hier ist die Mitarbeiter ID">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                    <Label fx:id="profilPasswort" alignment="CENTER" layoutY="93.0" prefHeight="62.0" prefWidth="302.0" text="Hier ist das Passwort">
                                       <font>
                                          <Font name="TeleNeo Office Regular" size="22.0" />
                                       </font>
                                    </Label>
                                 </children>
                              </Pane>
                              <Pane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
                                 <children>
                                    <ChoiceBox fx:id="reservierungenAusklapp" layoutX="74.0" layoutY="83.0" prefHeight="32.0" prefWidth="182.0" />
                                 </children>
                              </Pane>
                           </children>
                        </GridPane>
                     </children>
                  </VBox>
               </children>
            </Pane>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

所以问题是:
通过这个给定的代码,我可以为fmxl标签设置一个值,正如您在控制器2中看到的,方法:“label()”。只有在第一个控制器中声明问题场景的控制器时才有可能使用:“loaderprofil.setcontroller(controllerprofiluebersicht)”(它不在fxml文件中声明),然后加载fxml文件。但是如果我这样做了,我就不能在第二个场景中使用fxml按钮的动作事件,因为fxml文件希望我在它的控制器中实现这些方法,而fxml中没有给出这些方法。
另一方面,如果我在fxml文件中声明它,我就不能为fxml标签设置一个值,因为它们是“null”。
我正在寻找一个解决方案,在那里我可以设置值的fxml对象,以及使用上的行动方法。
我感谢任何帮助:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题