JavaFX 标签未使用 setText() 更新

标签 java javafx controller fxml scenebuilder

我进行了大量搜索,但似乎无法找到解决我的问题的方法,我正在尝试让标签 selectedName 在用户在 Intro.fxml 中输入他/她的名字时进行更新并出现在 Main.fxml 上。但是它仍然显示为在初始化方法中设置的 default_user。不可否认,代码有点乱,但这只是我正在做的一个小项目。

LogicController.java 不是整个文件

public class LogicController{ 

/** CLASS OBJECTS
 * 
 */

private LoadWords loadWords = new LoadWords();
private GameLogic game = new GameLogic();

/** AUDIO FILES 
 * items used for audio
 * All wavs are free
 * provided on freesound.org
 * **/

final URL resource = getClass().getResource("sounds/beep.wav");
final AudioClip menuClick = new AudioClip(resource.toString());

/** OTHER DECLARATIONS
 * other variables
 * 
 * 
 */

@FXML public ChoiceBox<String> difficultyBox = new ChoiceBox<String>();
@FXML public ObservableList<String> difficultyBoxList = FXCollections.observableArrayList("Easiest", "Easy", "Medium", "Hard");
@FXML public String selectedDifficulty; 
@FXML public TextField myName;
@FXML public Label selectedName = new Label(); 

@FXML  
public void initialize()
{
  difficultyBox.setItems(difficultyBoxList);
  selectedName.setText("default_player");


}


@FXML
public void loadSerial(MouseEvent mouseEvent) { 

    menuClick.play();
    loadWords.startSequential();    

    game.setAllLists(loadWords.getAllLists());
    game.setDifficulty(difficultyBox.getSelectionModel().getSelectedItem().toString());
    game.setName(myName.getText());
    selectedName.setText(game.getName().toString());

    try{

        Node  source = (Node)  mouseEvent.getSource(); 
        Stage intro  = (Stage) source.getScene().getWindow();
        intro.close();          

        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); 
        root.setId("pane");

        Scene scene = new Scene(root, 900, 506);
        Stage primaryStage = new Stage();

        primaryStage.setTitle("x");
        scene.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();

      }catch(Exception e) {
          e.printStackTrace();
      }


}

Intro.fxml

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

<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="fx" prefHeight="538.0" prefWidth="923.0" style="-fx-background-color: #4f0f41;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.LogicController">
   <children>
      <StackPane prefHeight="538.0" prefWidth="923.0">
         <children>
            <Pane prefHeight="545.0" prefWidth="929.0">
               <children>
                  <Label layoutX="265.0" layoutY="101.0" prefHeight="103.0" prefWidth="394.0" style="-fx-font-family: Paperland; -fx-font-size: 90;" stylesheets="@application.css" text="FANGMAN" textFill="WHITE">
                     <effect>
                        <Glow />
                     </effect>
                     <font>
                        <Font size="66.0" />
                     </font>
                  </Label>
                  <Label layoutX="265.0" layoutY="69.0" prefHeight="65.0" prefWidth="79.0" style="-fx-font-family: Paperland; -fx-font-size: 40;" stylesheets="@application.css" text="THE" textFill="WHITE">
                     <effect>
                        <Glow />
                     </effect>
                  </Label>
                  <Label layoutX="694.0" layoutY="24.0" text="An adaptation by Benjamin Meysner" textFill="WHITE" />
                  <FlowPane layoutX="266.0" layoutY="288.0" prefHeight="225.0" prefWidth="378.0">
                     <children>
                        <GridPane prefHeight="230.0" prefWidth="398.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" maxWidth="171.0" minWidth="10.0" prefWidth="171.0" />
                            <ColumnConstraints hgrow="SOMETIMES" maxWidth="265.0" minWidth="10.0" prefWidth="64.0" />
                              <ColumnConstraints hgrow="SOMETIMES" maxWidth="233.0" minWidth="0.0" prefWidth="152.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints />
                            <RowConstraints maxHeight="51.0" minHeight="5.0" prefHeight="48.0" />
                            <RowConstraints maxHeight="172.0" minHeight="10.0" prefHeight="109.0" vgrow="SOMETIMES" />
                              <RowConstraints maxHeight="78.0" minHeight="10.0" prefHeight="41.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                           <children>
                              <ImageView id="zomb" fx:id="zomb" fitHeight="150.0" fitWidth="200.0" onMouseClicked="#loadSerial" onMouseEntered="#zombieHover" pickOnBounds="true" preserveRatio="true" style="-fx-cursor: hand;" styleClass="zomb" GridPane.rowIndex="2">
                                 <image>
                                    <Image url="@images/chtr1.png" />
                                 </image>
                                 <effect>
                                    <Glow />
                                 </effect>
                              </ImageView>
                              <ImageView id="skul" fitHeight="150.0" fitWidth="200.0" onMouseClicked="#loadParallel" onMouseEntered="#skulHover" pickOnBounds="true" preserveRatio="true" style="-fx-cursor: hand;" styleClass="skul" GridPane.columnIndex="2" GridPane.rowIndex="2">
                                 <image>
                                    <Image url="@images/chtr3.png" />
                                 </image>
                                 <effect>
                                    <Glow />
                                 </effect>
                              </ImageView>
                              <Label alignment="CENTER" style="-fx-font-family: Paperland;" text="OR" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="2">
                                 <font>
                                    <Font size="50.0" />
                                 </font>
                                 <effect>
                                    <Glow />
                                 </effect>
                              </Label>
                              <Label text="Serial" textFill="WHITE" GridPane.rowIndex="3" />
                              <Label text="Parallel" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                           </children>
                        </GridPane>
                     </children>
                  </FlowPane>
                  <ImageView fitHeight="104.0" fitWidth="113.0" layoutX="156.0" layoutY="90.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@images/drac.png" />
                     </image>
                  </ImageView>
                  <Label layoutX="252.0" layoutY="262.0" prefHeight="52.0" prefWidth="137.0" text="Now enter your name..." textFill="WHITE" />
                  <Label layoutX="252.0" layoutY="203.0" prefHeight="78.0" prefWidth="137.0" text="To begin a new game, select a difficulty setting..." textFill="WHITE" wrapText="true" />
                  <TextField fx:id="myName" layoutX="405.0" layoutY="276.0" promptText="Enter name...">
                     <effect>
                        <DropShadow height="71.4" radius="30.7525" spread="0.13" width="53.61" />
                     </effect></TextField>
                  <Label layoutX="115.0" layoutY="281.0" prefHeight="78.0" prefWidth="137.0" text="Finally select a load option (Serial or Parallel)" textFill="WHITE" wrapText="true" />
                  <ChoiceBox fx:id="difficultyBox" layoutX="404.0" layoutY="225.0" prefWidth="150.0" value="Easiest">
                     <effect>
                        <DropShadow height="0.0" radius="29.2125" spread="0.19" width="118.85" />
                     </effect>
                  </ChoiceBox>
               </children>
            </Pane>
         </children>
      </StackPane>
   </children>
</AnchorPane>

Main.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<AnchorPane style="-fx-background-color: #4f0f41;" stylesheets="@application.css" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.LogicController">
   <children>
      <Pane prefHeight="506.0" prefWidth="900.0" stylesheets="@application.css">
         <children>
            <ImageView fitHeight="197.0" fitWidth="183.0" layoutX="35.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@images/drac.png" />
               </image>
            </ImageView>
            <Label layoutX="37.0" layoutY="223.0" text="Welcome" textFill="WHITE" />
            <Label fx:id="selectedName" layoutX="97.0" layoutY="218.0" prefHeight="27.0" prefWidth="121.0" text="  " textFill="WHITE">
               <font>
                  <Font size="13.0" />
               </font>
               <effect>
                  <Glow />
               </effect>
            </Label>
            <Pane layoutX="24.0" layoutY="211.0" opacity="0.45" prefHeight="127.0" prefWidth="200.0" style="-fx-background-color: BLACK;">
               <children>
                  <GridPane layoutX="13.0" layoutY="34.0" prefHeight="81.0" prefWidth="153.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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Games Won" textFill="WHITE" />
                        <Label text="Games Lost" textFill="WHITE" GridPane.rowIndex="1" />
                        <Label text="Win Ratio" textFill="WHITE" GridPane.rowIndex="2" />
                        <Label text="0" textFill="WHITE" GridPane.columnIndex="1" />
                        <Label text="0" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <Label text="0%" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                     </children>
                  </GridPane>
               </children>
            </Pane>
            <Pane layoutX="246.0" layoutY="61.0" opacity="0.45" prefHeight="421.0" prefWidth="633.0" style="-fx-background-color: BLACK;" />
            <Button layoutX="355.0" layoutY="22.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="100.0" text="save game" />
            <Pane layoutX="25.0" layoutY="373.0" opacity="0.45" prefHeight="104.0" prefWidth="200.0" style="-fx-background-color: BLACK;" />
            <Button layoutX="244.0" layoutY="22.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="100.0" text="load game" />
            <Button layoutX="469.0" layoutY="22.0" mnemonicParsing="false" onAction="#newWord" prefHeight="26.0" prefWidth="100.0" text="new word" />
            <Label layoutX="28.0" layoutY="484.0" text="message box" textFill="WHITE" />
            <Label layoutX="28.0" layoutY="346.0" text="statistics" textFill="WHITE" />
            <Label layoutX="246.0" layoutY="484.0" text="game view" textFill="WHITE" />
         </children>
      </Pane>
   </children>
</AnchorPane>

图片在这里 intro.fxml main.fxml 任何帮助将不胜感激!

最佳答案

你的变量已经被注解了 @FXML 所以不需要实例化它:

//Wrong
@FXML public Label selectedName = new Label ();

//Correct
@FXML public Label selectedName;

编辑:

同样适用于其他带注释的项目并尝试实现 Initializable :

LogicController implements Initializable{

关于JavaFX 标签未使用 setText() 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41243135/

相关文章:

java - 使用 JPQL 创建查询——查询语法异常

java - 如何更改 slick2d 左上角图标

java - 修改java/php

通过可编辑单元格的 JavaFX 选项卡

JavaFX Maven 插件部署 .deb 而不是 .exe 文件

asp.net-mvc-3 - 模拟 Request.QueryString 用于单元测试并针对 View 进行断言

java - 如何使用开源工具检查 PDF-A 1b 合规性?

java - jdeps 在 fat jar 中使用 Java 11 和 JavaFX 失败

javascript - 无法让 ng-repeat 列出对象中的用户 |没有错误信息

asp.net-mvc - ASP.Net MVC : Check if URL is Authorized