Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX标签未使用setText()更新_Java_Javafx_Controller_Fxml_Scenebuilder - Fatal编程技术网

JavaFX标签未使用setText()更新

JavaFX标签未使用setText()更新,java,javafx,controller,fxml,scenebuilder,Java,Javafx,Controller,Fxml,Scenebuilder,我已经做了很多搜索,似乎找不到解决问题的方法,我试图在用户在Intro.fxml中输入他/她的名字并出现在Main.fxml中时更新标签selectedName。但是,它仍然显示为在initialize方法中设置的默认用户。诚然,代码有点混乱,但这只是我正在做的一个小项目 LogicController.java不是整个文件 public class LogicController{ /** CLASS OBJECTS * */ private LoadWords loadWords

我已经做了很多搜索,似乎找不到解决问题的方法,我试图在用户在Intro.fxml中输入他/她的名字并出现在Main.fxml中时更新标签selectedName。但是,它仍然显示为在initialize方法中设置的默认用户。诚然,代码有点混乱,但这只是我正在做的一个小项目

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();
      }


}
公共类逻辑控制器{
/**类对象
* 
*/
私有LoadWords LoadWords=新LoadWords();
private GameLogic game=新GameLogic();
/**音频文件
*用于音频的项目
*所有WAV都是免费的
*在freesound.org上提供
* **/
最终URL resource=getClass().getResource(“sounds/beep.wav”);
final AudioClip menuClick=新的音频剪辑(resource.toString());
/**其他声明
*其他变量
* 
* 
*/
@FXML public ChoiceBox-difficultyBox=新建ChoiceBox();
@FXML public observebleList difficultyBoxList=FXCollections.observebleRaylist(“最简单”、“容易”、“中等”、“难”);
@FXML公共字符串选择困难;
@FXML公共文本字段myName;
@FXML公共标签selectedName=新标签();
@FXML
公共无效初始化()
{
设置项(困难框列表);
selectedName.setText(“默认播放器”);
}
@FXML
public void loadSerial(MouseEvent MouseEvent){
menuClick.play();
loadWords.startSequential();
game.setAllList(loadWords.getAllList());
game.setcombility(困难箱.getSelectionModel().getSelectedItem().toString());
game.setName(myName.getText());
selectedName.setText(game.getName().toString());
试一试{
Node source=(Node)mouseEvent.getSource();
Stage intro=(Stage)source.getScene().getWindow();
介绍close();
父根=FXMLLoader.load(getClass().getResource(“Main.fxml”);
root.setId(“窗格”);
场景=新场景(根,900,506);
阶段primaryStage=新阶段();
初级阶段。设置标题(“x”);
scene.getStylesheets().addAll(this.getClass().getResource(“application.css”).toExternalForm());
primaryStage.SetResizeable(假);
初级阶段。场景(场景);
primaryStage.show();
}捕获(例外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>
<?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>


非常感谢您的帮助!

您的变量已被注释
@FXML
,因此无需实例化它:

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

//Correct
@FXML public Label selectedName;
编辑:

对于其他带注释的项也一样,并尝试实现初始化:

LogicController implements Initializable{

Intro.fxml
一起使用的控制器实例与与与
Main.fxml
一起使用的实例不同。在加载fxml之前,需要指定控制器实例以使用相同的实例:

FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
loader.setController(this);
Parent root = loader.load();

// fill data to the field just created when loading the fxml
selectedName.setText(game.getName().toString());
(这需要从
Main.fxml
中删除
fx:controller
属性)

请注意,这种初始化方式会被调用两次。最好使用单独的控制器

有多种方法可以将信息传递给fxml文件的控制器,这些方法可能比上述方法更适合,例如访问由
FXMLLoader
创建的控制器实例


请注意,初始化应该从fxml注入以防止NPE的字段并不是一个好主意,因为这只是错误的症状,并且初始化字段时使用的值与从fxml创建的对象没有连接。

如果将其取出,则会出现空指针异常
code
,原因是:java.lang.nullapplication.LogicController.initialize(LogicController.java:56)…27更多尝试发布完整代码或仅发布
LogicController
的第56行仍然会为同一行引发空指针异常,selectedName.setText(“默认\U播放器”);第56行:selectedName.setText(“默认\U播放器”);感谢您的帮助两个FXML文件都有相同的控制器。您不需要在两个文件中都使用
fx:controller
。这里有一个关于@Shashwat的问题实际上,它们“没有相同的控制器”。每次加载这两个FXML文件中的任何一个时,都会创建一个新控制器:但是,这两个FXML文件的控制器都来自同一个类。(同一对象与同一类中的两个对象之间有很大区别。)这样做通常是非常糟糕的:每个FXML文件都应该有不同的控制器类。我从Label selectedName中删除了实例化位,并在loadSerial方法中添加了以下内容。
code
try{Node source=(Node)mouseEvent.getSource();Stage intro=(Stage)source.getScene().getWindow();intro.close();fxmloader=new fxmloader(getClass().getResource(“Main.fxml”);loader.setController(this);父根=loader.load();
code
。仍然获取标签的空指针,并从main中删除fx:controller属性。fxml@benjay_ukFWIW I甚至不会尝试为两个不同的FXML文件使用同一个控制器或来自同一类的控制器em当您需要时。@benjay_uk如果您使用相同的控制器实例,则每次对注入字段的访问都需要在调用
load()之后进行
。在这种情况下,您是从
initialize
方法访问字段,该方法无法初始化fxml未注入的内容。顺便说一句:您在fxml中分配了内容,那么为什么要用常量覆盖
initialize
方法中的值呢?