2016-12-20 2 views
2

私は多くの検索を行っていますが、私の問題の解決策を見つけることができない場合は、ラベルを取得しようとしていますselectedNameは、 Intro.fxmlであり、Main.fxmlに表示されます。ただし、initializeメソッドで設定されているdefault_userとして表示されます。確かにコードはちょっと混乱していますが、それは私が取り組んでいるちょっとしたプロジェクトです。JavaFXのラベルがsetText()で更新されない

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 ご了承ください。

+0

両方のFXMLファイルに同じコントローラがあります。両方とも 'fx:controller 'は必要ありません。実際には、彼らは "同じコントローラを持っていない"と述べた[それは[その](上の質問)です(http://stackoverflow.com/questions/30464857/one-controller-to-2-fxmls-javafx) –

+1

@Shashwat。これらのFXMLファイルのいずれかがロードされるたびに、新しいコントローラが作成されますが、両方のFXMLファイルのコントローラは同じクラスのコントローラです。 ( "同じオブジェクトであること"と "同じクラスに属する2つのオブジェクト"の間には大きな違いがあります)。これは非常に悪い考えです.FXMLファイルごとに異なるコントローラ*クラス*が必要です。 –

答えて

1

あなたの変数はすでにそれをインスタンス化する@FXMLので不要注釈を付けていない:

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

//Correct 
@FXML public Label selectedName; 

編集:他の注釈付きのアイテムの

同じで、初期化可能を実装してみてください。

LogicController implements Initializable{ 
+0

私はそれを取り出す場合は、私がnullポインタ例外によって引き起こさ 'code' 取得:application.LogicController.initializeでjava.lang.NullPointerExceptionが \t(LogicController.java:56) \tを... 27もっと –

+0

は、投稿してくださいあなたの 'LogicController'の完全なコードかちょうど56行 –

+0

同じ行、selectedName.setText(" default_player ")のために投げられたヌルポインタ例外をまだ得ます; –

2

コントローラを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属性を除去する必要があります。)

(注)この方法の初期化が2回呼び出されていること。それは、別々のコントローラを使用する方がよいでしょう。

fxmlファイルのコントローラに情報を渡す方法は複数ありますが、これは上記の方法よりも適しています。 FXMLLoaderによって作成されたコントローラインスタンスにアクセスします。

fxmlから注入する必要があるフィールドを初期化すると、NPEを防止することはほとんどありません。これはエラーの単なる症状であり、フィールドを初期化する値は作成されたオブジェクトに接続していないfxml。

+0

助けてくれてありがとう、私はそれを試してみましょう! –

+0

ラベルselectedNameからインスタンス化ビットを削除し、loadSerialメソッドの中で以下を追加しました。 'code' {\t ノードソース=(ノード)mouseEvent.getSource(); ステージイントロ=(ステージ)source.getScene()。getWindow(); intro.close(); \t \t FXMLLoaderローダ=新しいFXMLLoader(のgetClass()のgetResource( "Main.fxml")。)。 loader.setController(this); 親ルート= loader.load(); 'code'は...まだラベル –

+0

にnullポインタもFXを削除されます:FWIW @benjay_uk main.fxml –

関連する問題