Это 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, 29 апреля 2015 г., 02:25:11)
Java-версия:
Java-версия "1.8.0_191"
Java (TM) SE Runtime Environment (сборка 1.8.0_191-b12)
Java HotSpot (TM) 64-разрядная серверная виртуальная машина (сборка 25.191-b12, смешанный режим)
Я запускаю код на Lubuntu на основе Ubuntu 18.04.1 LTS.