2017-05-27 8 views
0

setTextメソッドを使用してラベルのテキストを変更したいのですが、fx:idパラメータを定義しても変更できません。JavaFXアプリケーションのラベルテキストを変更できませんでした

Welcome.fxml

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.PasswordField?> 
<?import javafx.scene.control.TextField?> 
<?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.RowConstraints?> 
<?import javafx.scene.text.Font?> 

<AnchorPane id="AnchorPane" prefHeight="247.0" prefWidth="615.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="banking.manager.WelcomeController"> 
    <children> 
     <Label fx:id="welcomeLabel" layoutX="26.0" layoutY="23.0" prefHeight="29.0" prefWidth="224.0" text="Banking Manager"> 
     <font> 
      <Font size="24.0" /> 
     </font></Label> 
     <Button fx:id="exitButton" layoutX="526.0" layoutY="192.0" mnemonicParsing="false" onAction="#handleCloseRequest" prefHeight="27.0" prefWidth="75.0" text="Close" /> 
     <Button fx:id="signButton" layoutX="440.0" layoutY="192.0" mnemonicParsing="false" onAction="#handleSignInRequest" prefHeight="27.0" prefWidth="75.0" text="Sign In" /> 
     <GridPane layoutX="26.0" layoutY="81.0" prefHeight="79.0" prefWidth="353.0"> 
     <columnConstraints> 
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="138.0" minWidth="10.0" prefWidth="110.0" /> 
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="243.0" minWidth="10.0" prefWidth="243.0" /> 
     </columnConstraints> 
     <rowConstraints> 
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
     </rowConstraints> 
     <children> 
      <Label prefHeight="18.0" prefWidth="93.0" text="Username:" GridPane.halignment="CENTER" GridPane.valignment="CENTER"> 
       <font> 
        <Font size="14.0" /> 
       </font> 
      </Label> 
      <Label prefHeight="18.0" prefWidth="93.0" text="Password:" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER"> 
       <font> 
        <Font size="14.0" /> 
       </font> 
      </Label> 
      <TextField fx:id="usernameField" prefHeight="27.0" prefWidth="247.0" GridPane.columnIndex="1" /> 
      <PasswordField fx:id="passwordField" prefHeight="27.0" prefWidth="251.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
     </children> 
     </GridPane> 
     <ImageView fitHeight="117.0" fitWidth="151.0" layoutX="440.0" layoutY="38.0" pickOnBounds="true" preserveRatio="true"> 
     <image> 
      <Image url="@safebox.png" /> 
     </image> 
     </ImageView> 
     <Label fx:id="informationLabel" text="Log in" layoutX="26.0" layoutY="197.0" prefHeight="17.0" prefWidth="353.0" /> 
    </children> 
</AnchorPane> 

WelcomeController.java

import java.net.URL; 
import java.sql.ResultSet; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 

import javafx.scene.control.Alert; 
import javafx.scene.control.Alert.AlertType; 
import javafx.scene.control.Label; 

/** 
* 
* @author apple 
*/ 
public class WelcomeController implements Initializable { 

    @FXML 
    private Button signButton; 
    private Button exitButton; 
    private Label informationLabel = new Label(); 
    private TextField usernameField = new TextField(); 
    private PasswordField passwordField = new PasswordField(); 

    @FXML 
    private void handleSignInRequest(ActionEvent event) throws Exception { 

     informationLabel.setText("Please wait while request is getting processed..."); 
     // ... 

私は別のイベントハンドラでそれを移動しようとしたん、まだ何も。

答えて

2

FXMLLoaderには@FXMLと注釈されていないため、informationLabelにはアクセスできません。
宣言で新しいLabelを使用してフィールドを初期化すると、いずれのシーンにも表示されないLabelを作成するだけです。代わりに、次のように宣言する必要があります。

@FXML 
private Label informationLabel; 
関連する問題