问题描述
我在Javafx的两个场景之间尝试了按钮事件。
I've been trying button events between 2 scenes in Javafx.
这是Start.fxml(在视图包中):
This is Start.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.StartController">
<children>
<Label fx:id="lbl1" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt1" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn1" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn1Click" text="Click" />
</children>
</AnchorPane>
这是Preserences.fxml(在视图包中):
This is Preserences.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.PreferencesController">
<children>
<Label fx:id="lbl2" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt2" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn2" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn2Click" text="Click" />
</children>
</AnchorPane>
这是Main.fxml,类似于以前2个文件的容器(在视图包中) :
This is Main.fxml which is like a container for the previous 2 files.(in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="481.0" prefWidth="468.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.MainController">
<children>
<TabPane layoutX="14.0" layoutY="14.0" prefHeight="481.0" prefWidth="468.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab closable="false" text="Start">
<content>
<fx:include source="Start.fxml" />
</content>
</Tab>
<Tab closable="false" text="Preferences">
<content>
<fx:include source="Preferences.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
这是StartController.java(在控制器包中):
This is StartController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class StartController implements Initializable {
@FXML public static Label lbl1;
@FXML public static TextField txt1;
@FXML public static Button btn1;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML private void btn1Click(ActionEvent e){
System.out.println("Button 1 clicked");
lbl1.setText(PreferencesController.txt2.getText());
}
}
这是PreferencesController.java控制器包):
This is PreferencesController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class PreferencesController implements Initializable {
@FXML public static Label lbl2;
@FXML public static TextField txt2;
@FXML public static Button btn2;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML private void btn2Click(ActionEvent e){
System.out.println("Button 2 clicked");
lbl2.setText(StartController.txt1.getText());
}
}
这是MainController.java控制器包):
This is MainController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class MainController implements Initializable{
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
}
最后,这是Main.java在应用程序包中:
Finally, this is Main.java in application package:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
但是当我点击按钮,错误:
But when I clicked the buttons, it gives me the errors:
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException...
...
Caused by: java.lang.NullPointerException
at controller.StartController.btn1Click(StartController.java:26)
... 61 more"
请帮助我! :
推荐答案
确定,以便NPE被抛到这一行:
OK so the NPE is being thrown on this line:
lbl1.setText(PreferencesController.txt2.getText());
$ b b
在基本的Java层面,只有两种方法可以实现:
At the basic Java level, are only two ways that you could get that to happen:
- 当
PreferencesController.txt2 时,> lnl1
是null
或 - c $ c>是
null
。
when
lnl1
isnull
, orwhen
PreferencesController.txt2
isnull
.
因为
PreferencesController.txt2
是一个静态字段。
我不熟悉JavaFX或FXML,但我怀疑你的错误是声明字段为
static
。我怀疑这导致FXML机制不注入引用因此这些变量保持为 null
...并且您获得了NPE。
I'm not familiar with JavaFX or FXML, but I suspect that your mistake is declaring the fields as
static
. I suspect that this causes the FXML mechanisms to not inject the reference to the UI component correctly. So those variables remain as null
... and you get an NPE.
确认是此处:。 (感谢链接@James_D)
Confirmation is here: http://stackoverflow.com/a/23109125/139985. (Thanks for the link @James_D)
这篇关于java.lang.NullPointerException在处理控制在2个控制器之间 - Javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!