无法操纵JavaFX中的Label

时间:2013-12-10 18:05:36

标签: java user-interface javafx

每次用户访问该页面时,我都会尝试更改JavaFX软件GUI标签的文本。为此,我必须能够更改该标签的值,例如将其解析为变量并执行这些操作。

问题是,由于标签都是用FXML编写的,我不知道如何在Java上正确使用它。我试过了:

@FXML public Label WordID;

为了使标签(具有“WordID”作为ID)成为对象,但我失败了。当我这样做时,该程序将不再进入该框架。我该怎么办?

代码:

public class Screen3Controller implements Initializable, ControlledScreen {

ScreensController myController;

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

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void goToScreen1(ActionEvent event){
   myController.setScreen(ScreensFramework.screen1ID);
}

@FXML
private void goToScreen2(ActionEvent event){
   myController.setScreen(ScreensFramework.screen2ID);
}

}

FXML:

<Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
  <font>
    <Font name="Avenir Roman" size="40.0" />
  </font>
</Label>

Screen2的按钮侦听器:

@FXML
private void goToScreen3(ActionEvent event){
   myController.setScreen(ScreensFramework.screen3ID);
   WordID.setText("LOl");
}

错误讯息:

  

Glass在 - [GlassViewDelegate sendJavaMouseEvent:]检测到未解决的Java异常:src / com / sun / mat / ui / GlassViewDelegate.m:543   线程“JavaFX Application Thread”中的异常java.lang.RuntimeException:   ...   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)       at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)       at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)       at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:250)       at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:173)       at java.security.AccessController.doPrivileged(Native Method)       在javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1444)       ......还有40多个   引起:java.lang.NullPointerException       在screensframework.Screen3Controller.mostrarPalavra(Screen3Controller.java:89)       ......还有50个

1 个答案:

答案 0 :(得分:1)

要以编程方式使用Label,您需要做的就是在Controller文件中声明一个与.fxml文件中声明的id相同的变量:

<强> YourFXML.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.sample.Screen3Controller ">
    ...
    <Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
      <font>
        <Font name="Avenir Roman" size="40.0" />
      </font>
    </Label>
    ...
</AnchorPane>

com.sample.Screen3Controller必须引用.java包中的现有com.sample文件。

Screen3Controller .java

import javafx.scene.control.Label;  //make sure you're importing the JavaFX Label

public class Screen3Controller implements Initializable, ControlledScreen {

    ScreensController myController;
    @FXML private Label WordID;

    ...

    WordID.setText("some text...");

    ...
}