【问题标题】:How to change label text from different fxml如何从不同的 fxml 更改标签文本
【发布时间】:2014-12-29 04:46:44
【问题描述】:

实际上我正在尝试更改 FXML 中标签上的文本

这是控制器的代码

public class ShowDeleteController implements Initializable {

@FXML
Label labelType;
@FXML
Label labelID;
@FXML
Label labelName;
@FXML
Label LabelBasedOnTypeText;
@FXML
Label LabelBasedOnType;

@Override
public void initialize(URL url, ResourceBundle rb) {
    /*Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
    labelType.setText(prefs.get("userName", labelType.getText()));*/ tried with this but don't work
}    

public void onClickCancel(ActionEvent event) {
    Node source = (Node) event.getSource();
    Stage stage = (Stage) source.getScene().getWindow();        
    stage.close();
}

public void SettingText(String TypeData, String ID, String Name, String Add, String AddText, ActionEvent event) {

    labelType.setText(TypeData);
    labelID.setText(ID);
    labelName.setText(Name);
    LabelBasedOnTypeText.setText(AddText);
    LabelBasedOnType.setText(Add);
}    

}

这是我调用 fxml 的代码

 public void showDeletedData(String type, String ID, String Name, String Add, String AddText, ActionEvent event) throws Exception{
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("PopUp/showDelete.fxml"));
        AnchorPane page = (AnchorPane) loader.load();
        ShowDeleteController controller = loader.getController();
        controller.SettingText(type, ID, Name, Add, AddText, event);            
        Stage PopUpStage = new Stage();
        PopUpStage.setTitle("Registro Eliminado");
        PopUpStage.initModality(Modality.APPLICATION_MODAL);
        PopUpStage.centerOnScreen();
        PopUpStage.setResizable(false);
        Scene scene = new Scene(page);
        PopUpStage.setScene(scene);

        PopUpStage.showAndWait();


    } catch (Exception ex) {
        System.out.println("Internal error: " + ex.getMessage());
    }
}

所以实际上只是想知道是否有必要初始化标签,因为我尝试不设置标签上的文本并且它可以工作,但是,当然文本仅默认显示。

希望你能帮我解决这个问题,对不起我的英语不好

【问题讨论】:

  • 我解决了只是标签上的名字有错误,一个很简单的错误

标签: javafx label fxml


【解决方案1】:

代码已经成功了

public class ShowDeleteController implements Initializable {

@FXML
Label LabelType;
@FXML
Label LabelID;
@FXML
Label LabelName;
@FXML
Label LabelBasedOnTypeText;
@FXML
Label LabelBasedOnType;

public void onClickCancel(ActionEvent event) {
    Node source = (Node) event.getSource();
    Stage stage = (Stage) source.getScene().getWindow();
    stage.close();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}

public void setTextDeleted(String Type, String ID, String Name, String BasedType, String BasedTypeText) {
    LabelType.setText(Type);
    LabelID.setText(ID);
    LabelName.setText(Name);
    LabelBasedOnTypeText.setText(BasedTypeText);
    LabelBasedOnType.setText(BasedType);
}

}

因为 FXML 文件以这种方式获得了标签的名称

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" minHeight="300.0" minWidth="450.0" prefHeight="300.0"     prefWidth="450.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="App.PopUp.ShowDeleteController">
  <children>
  <Label layoutX="151.0" layoutY="28.0" text="Se Eliminó un Registro"     AnchorPane.leftAnchor="151.0" AnchorPane.rightAnchor="150.0" AnchorPane.topAnchor="30.0" />
  <Label layoutX="32.0" layoutY="84.0" text="Tipo" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="100.0" />
  <Label layoutX="47.0" layoutY="138.0" text="ID" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="130.0" />
  <Label layoutX="37.0" layoutY="200.0" text="Nombre" AnchorPane.leftAnchor="30.0"        AnchorPane.topAnchor="160.0" />
  <Label fx:id="LabelBasedOnType" layoutX="37.0" layoutY="255.0" text="LabelBasedOnType" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="190.0" />
  <Button layoutX="175.0" layoutY="241.0" minWidth="100.0" mnemonicParsing="false" onAction="#onClickCancel" prefWidth="100.0" text="Aceptar" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="175.0" AnchorPane.rightAnchor="175.0" />
  <Label fx:id="LabelBasedOnTypeText" layoutX="175.0" layoutY="190.0" text="Label" />
  <Label fx:id="LabelName" layoutX="175.0" layoutY="160.0" text="Label" />
  <Label fx:id="LabelID" layoutX="175.0" layoutY="130.0" text="Label" />
  <Label fx:id="LabelType" layoutX="175.0" layoutY="100.0" text="Label" />
  </children>
  </AnchorPane>

现在可以了 =)

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多