Label's SetText JavaFX not working

时间:2017-08-28 12:40:32

标签: java javafx label

The problem is label's setText() is not woking. It happens when the scene shows up, but the initial label text doesn't change. My setText label won't work when I run the code. What can I do? Is this a wrong logic?

Main Class:

   package Main;

public class MainMenu extends Application {
    public static String t = "ебучий лейбл";
    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
//          test_timer.setText("lalala");
            System.out.print("dwdw");
        }
    }

    public static Locale locale = new Locale("ru");
    Timer myTimer;
//    @FXML
//    public static Label test_timer;
    @FXML
    Label test_timer = new Label();
    @Override
    public void start(Stage primaryStage) throws Exception{
//        MainMenuController.language_chg = "ru";
        FXMLLoader fxmlLoader = new FXMLLoader();

        fxmlLoader.setLocation(getClass().getResource("../f_XML/Menu.fxml"));
        fxmlLoader.setResources(ResourceBundle.getBundle("Localization.Locate", locale));
//      fxmlLoader.setResources(ResourceBundle.getBundle("Localization.Locate", locale(MainMenuController.language_chg)));

        Parent root = fxmlLoader.load();
        fxmlLoader.getController();
        test_timer.setText("dwdwdwdwdwd");

        Scene scene = new Scene(root, 1280, 720 );
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);

        primaryStage.setResizable(false);
        primaryStage.show();        

        MyTimerTask myTask = new MyTimerTask();
        myTimer = new Timer();
        myTimer.schedule(myTask, 1000, 1000);       
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Controller:

@FXML
    public static Label test_timer = new Label();

    public void btnActionLanguage(ActionEvent actionEvent) {     
        test_timer.setText("вв");    
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        test_timer.setText("Putin is God,Navalniy loh");
    }

FXML:

<HBox focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" styleClass="background" stylesheets="@../Stylesheets/StyleMenu.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.MainMenuController">
   <children>
      <VBox prefHeight="720.0" prefWidth="955.0" styleClass="foreground1" HBox.hgrow="ALWAYS">
         <HBox.margin>
            <Insets left="240.0" right="240.0" />
         </HBox.margin>
         <children>
            <AnchorPane fx:id="panel_1" prefHeight="179.0" prefWidth="800.0">
               <children>
                  <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="325.0" layoutY="23.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="23.0" AnchorPane.leftAnchor="325.0" AnchorPane.rightAnchor="325.0" AnchorPane.topAnchor="23.0">
                     <image>
                        <Image url="@../images/logo.png" />
                     </image>
                  </ImageView>
                  <Label fx:id="test_timer" layoutX="76.0" layoutY="111.0" text="00:00" />
                  <Label layoutX="694.0" layoutY="23.0" opacity="0.35" text="v.0.1.3.alpha" textFill="#aa00ff" />
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="111.0" prefWidth="800.0">
               <children>
                  <Button fx:id="btnNewWorld" layoutX="244.0" layoutY="56.0" mnemonicParsing="false" onAction="#new_world" prefHeight="89.0" prefWidth="312.0" styleClass="btn_1" text="%MainMenu.newGame" textFill="WHITE" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="244.0" AnchorPane.rightAnchor="244.0" AnchorPane.topAnchor="50.0">
                     <font>
                        <Font size="24.0" />
                     </font>
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </Button>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </AnchorPane>
            <AnchorPane prefHeight="90.0" prefWidth="200.0">
               <children>
                  <Button layoutX="244.0" layoutY="56.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="312.0" styleClass="btn_1" text="%MainMenu.continueGame" textFill="WHITE" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="244.0" AnchorPane.rightAnchor="244.0" AnchorPane.topAnchor="50.0">
                     <font>
                        <Font size="24.0" />
                     </font>
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </Button>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="90.0" prefWidth="200.0">
               <children>
                  <Button layoutX="244.0" layoutY="56.0" mnemonicParsing="false" prefHeight="69.0" prefWidth="312.0" styleClass="btn_1" text="%MainMenu.settingsGame" textFill="WHITE" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="244.0" AnchorPane.rightAnchor="244.0" AnchorPane.topAnchor="50.0">
                     <font>
                        <Font size="24.0" />
                     </font>
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </Button>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="90.0" prefWidth="200.0">
               <children>
                  <Button layoutX="244.0" layoutY="56.0" mnemonicParsing="false" onAction="#btnActionExit" prefHeight="69.0" prefWidth="312.0" styleClass="btn_1" text="%Main.exitGame" textFill="WHITE" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="244.0" AnchorPane.rightAnchor="244.0" AnchorPane.topAnchor="50.0">
                     <font>
                        <Font size="24.0" />
                     </font>
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </Button>
               </children>
            </AnchorPane>
            <Button mnemonicParsing="false" onAction="#btnActionLanguage" prefHeight="59.0" prefWidth="62.0" styleClass="language_btn" text="en" textFill="#fff8f8">
               <font>
                  <Font size="24.0" />
               </font>
               <VBox.margin>
                  <Insets bottom="20.0" left="20.0" />
               </VBox.margin>
            </Button>
         </children>
      </VBox>
   </children>
</HBox>

I have no idea why setText() does not work. Thank you for help.

0 个答案:

没有答案