找不到JavaFX的fxml错误 [英] Cannot locate the fxml error for JavaFX

查看:267
本文介绍了找不到JavaFX的fxml错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施此处所述的多语言支持解决方案。

I'm trying to implement solution for multi-language support as described here.

但我得到的是java.lang.NullPointerException:位置是必需的。
请在以下相关代码中指出缺陷。
谢谢

But I'm getting "java.lang.NullPointerException: Location is required." Please point out the flaw(s) in following relevant code. Thanks

MultiLanguageSupport.java

public class MultiLingualSupport extends Application {

Stage currentStage;
void setStage(Stage stage)
{
    currentStage=stage;
}
@Override
public void start(Stage stage) throws Exception {

    setStage(stage);
    replaceScene("LanguageUIFXML.fxml",this,new Locale("en","EN"));

}


public static void main(String[] args) {
    launch(args);
}
public void replaceScene(String fxml, Object aThis, Locale locale) //throws Exception
{
    System.out.println("Replacing Scene");
   try 
   {
       Parent root;
       FXMLLoader loader=new FXMLLoader();
       loader.setResources(ResourceBundle.getBundle("TextData.AllText", locale));
       root = loader.load(this.getClass().getResource(fxml),null, new JavaFXBuilderFactory());
       Scene scene=currentStage.getScene();
       if(scene==null)
       {
           scene=new Scene(root);
           currentStage.setScene(scene);
       }
       else
           currentStage.getScene().setRoot(root);

       currentStage.sizeToScene();
       currentStage.show();
   }
   catch (IOException ex) 
   {
       ex.printStackTrace();
   }


}
}

LanguageUIFXML.fxml

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="multilingualsupport.LanguageUIFXMLController">
  <children>
    <Label fx:id="lblDemo" layoutX="284.0" layoutY="72.0" prefHeight="26.0" prefWidth="144.0" text="%keyDemo" />
    <TextField fx:id="txtDemo" layoutX="284.0" layoutY="124.0" prefWidth="200.0" text="%keyTextField" />
    <TextArea fx:id="txtareaDemo" layoutX="284.0" layoutY="164.0" prefWidth="200.0" text="%keyTextArea" wrapText="true" />
    <Button fx:id="btnDemo" layoutX="356.0" layoutY="347.0" mnemonicParsing="false" text="%keyButton" />
    <ListView fx:id="listviewDemo" layoutX="35.0" layoutY="85.0" orientation="HORIZONTAL" prefHeight="218.0" prefWidth="221.0" />
  </children>
  <stylesheets>
    <URL value="@/css/langageuifxml.css" />
  </stylesheets>
</AnchorPane>

LanguageUIFXMLController.java

public class LanguageUIFXMLController implements Initializable {

private Label label;
@FXML
private Label lblDemo;

@FXML
private TextField txtDemo;
@FXML
private TextArea txtareaDemo;
@FXML
private Button btnDemo;
@FXML
private ListView<String> listviewDemo=new ListView<>();
private ResourceBundle bundle;

    private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    bundle=rb;
    lblDemo.setText(rb.getString("keyDemo"));
    txtDemo.setText(rb.getString("keyTextField"));
    txtareaDemo.setText(rb.getString("keyTextArea"));
    btnDemo.setText(rb.getString("keyButon"));



}    
}

错误消息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.javafx.main.Main.launchApp(Main.java:642)
at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
at multilingualsupport.MultiLingualSupport.replaceScene(MultiLingualSupport.java:58)
at multilingualsupport.MultiLingualSupport.start(MultiLingualSupport.java:35)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)

.properties文件定义于package TextData

.properties files are defined in package TextData

推荐答案

很抱歉这是2个拼写错误的结果&一个不正确的css路径。
必填更改:
在fxml文件中,
应该是什么:

Well sorry this was result of 2 spelling mistakes & one incorrect path for css. Required changes: In fxml file, what should've been:

<URL value="@/css/langageuifxml.css" />

实际上是

<URL value="@css/langageuifxml.css" />

这篇关于找不到JavaFX的fxml错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆