我想通过setText
方法更改标签文字,但即使在定义fx:id
参数后,我也无法这样做。
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...");
// ...
我试图在另一个事件处理程序中移动它,但仍然没有。
答案 0 :(得分:2)
informationLabel
无法访问FXMLLoader
,因为它未使用@FXML
进行注释。
通过在声明中使用新的Label
初始化字段,您只需创建一个未在任何场景中显示的Label
。相反它应该像这样声明:
@FXML
private Label informationLabel;