JavaFX - 如何在场景中编辑/使用变量

时间:2017-10-21 15:12:25

标签: java javafx dependencies fxml

如何更改JavaFX场景中标签的值?我试图想办法在按钮点击时更改标签的文本值,但它不起作用。 我为按钮和标签设置了 fx:id 并设置了它们的控制器( CPCS305_JAVAFX_FXML ,类本身。该按钮也链接到 handleButtonAction2 ()功能。

public class CPCS305_JAVAFX_FXML extends Application {

@FXML
Scene scene;
Stage stage;
Label main_label
Button login_button;
AnchorPane root;

@Override
public void start(Stage stage) throws Exception {
    root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

@FXML
private void handleButtonAction2(ActionEvent event) throws IOException, Exception {
    main_label.setText("Hello World");
}

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

错误报告是:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 45 moreCaused by: java.lang.NullPointerException
at cpcs305_javafx_fxml.CPCS305_JAVAFX_FXML.handleButtonAction2(CPCS305_JAVAFX_FXML.java:64)
... 55 more

我有2个其他FXML文件及其控制器,功能,但我将其留待以后使用。我想解决一个场景中无法通信的问题,然后转到多个场景。

对新学员的任何想法?

编辑:此课程的FXML文件 here

EDIT2:已修复。问题是错误的进口。很抱歉给大家带来麻烦,但感谢您的支持。

3 个答案:

答案 0 :(得分:0)

@FXML注释应该只在FXML文件中定义的元素之上,例如标签,按钮和锚定窗格。

尝试一下,让我们看看。

答案 1 :(得分:0)

您需要使用@FXML注释每个实例字段,以便由FXML Loader分配。如果不这样做,值将保持null。此外,FXML加载程序需要知道哪个对象是控制器。您已在fx:controller="cpcs305_javafx_fxml.CPCS305_JAVAFX_FXML"中在FXML中指定了这一点,但这意味着FXMLLoader将创建您的类的第二个实例,因为它确实有一种方法可以将它连接到您当前的类。您可以通过指定具有setController的控制器,通过setControllerFactory指定控制器工厂,或者通过将参数传递给采用静态函数的更多参数来执行此操作。

由于您已在FXML中定义了类,因此需要使用控制器工厂。 FXML加载器将要求控制器工厂实例化它作为参数提供的类的对象。如果您想使用已经实例化的类,只需忽略类参数并返回this

public class CPCS305_JAVAFX_FXML extends Application {
    @FXML // because there is a fx:id="main_label"  in the FXML
    private Label main_label;  // Java naming convention would be mainLabel

    @FXML // because there is a fx:id="login_button"  in the FXML
    private Button login_button; // Java naming convention would be loginButton

    @Override
    public void start(Stage stage) throws Exception {
       FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
       loader.setControllerFactory(controllerClass -> this); // 'this' object will be the controller.
       Parent root = loader.load();
       Scene scene = new Scene(root);
       stage.setScene(scene);
       stage.show();
    }

   @FXML // because there is a onAction="#handleButtonAction" in the FXML
   private void handleButtonAction() { // parameter is optional
      main_label.setText("Hello World");
   }
}

答案 2 :(得分:0)

我在Scene Builder上使用的按钮/标签上使用了错误的导入。我修复了那些进口,一切正常。

相关问题