Jython未加载fxml文件

时间:2018-11-09 19:29:27

标签: java python javafx fxml jython

这是test.fxml:

<?xml version="1.0" encoding="UTF-8"?>    
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Label text="It is all OK" BorderPane.alignment="CENTER" />
    </center>
</BorderPane>

我做了一个Main.java来启动窗口,如下所示:

import java.io.IOException;
import java.net.URL;
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 {
    public static void main(String args[]) { 
        launch(args); 
    } 

    public void start(Stage stage) {
        try {
            URL url = this.getClass().getResource("/test.fxml");
            Parent root = FXMLLoader.load(url);
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            // ignore
        }
    }
}

有效。

但是main.py无法启动,它几乎等于Main.java。

我为此文件制作了2个版本,它们会引发不同的错误。

这是第一个版本:

import sys

from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene

class Main(Application):
@classmethod
def main(cls, args):
    Main.launch(cls, args)

def start(self, stage):
    try:
        url = self.getClass().getResource('/test.fxml')
        root = FXMLLoader.load(url)
        scene = Scene(root)
        stage.setScene(scene)
        stage.show()
    except Exception as exc:
        pass

if __name__ == '__main__':
    Main.main(sys.argv)

显示:


    Exception in Application start method
    Traceback (most recent call last):
      File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in 
        Main.main(sys.argv)
      File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
        Main.launch(cls, args)
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.NullPointerException
        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)

    java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method

这是第二个版本:

import sys

from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene

class Main(Application):
@classmethod
def main(cls, args):
    Main.launch(cls, args)

def start(self, stage):
    try:
        url = self.getClass().getResource('/test.fxml')
        self.loader = FXMLLoader(url) # changed here
        root = self.loader.load()     # changed here
        scene = Scene(root)
        stage.setScene(scene)
        stage.show()
    except Exception as exc:
        pass

if __name__ == '__main__':
    Main.main(sys.argv)

我遇到另一个错误,即:


    Exception in Application start method
    Traceback (most recent call last):
      File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in 
        Main.main(sys.argv)
      File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
        Main.launch(cls, args)
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        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)

    java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method

我的错误在哪里?

所有文件都在同一文件夹中。

请帮帮我。谢谢。

Jython版本是:

Jython 2.7.0(默认值:9987c746f838,2015年4月29日,02:25:11)

Java版本是:

java版本“ 1.8.0_191” Java(TM)SE运行时环境(内部版本1.8.0_191-b12) Java HotSpot(TM)64位服务器VM(内部版本25.191-b12,混合模式)

我正在基于Ubuntu 18.04.1 LTS的Lubuntu上运行代码。

0 个答案:

没有答案