javafx打开窗口没有效果.应用程序启动时没有找到抛出类



好,要启动javafx应用程序,我们需要启动javafx应用程序。然后,如果我们想添加一个新窗口,我们可以简单地执行stageobj.show();对吧?

下面是我尝试创建新窗口的部分代码。我已经启动了我的应用程序,并想从我的应用程序中调用这段新代码。

//imports all needed class
public String plugstart(){
try{Application.launch();
net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
Stage stage = new Stage();
stage.setTitle("Date pick test");//scn.getChildren().add(obj);
stage.setScene(scn);
stage.show();
return "ok";}catch(Exception e){java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw =new java.io.PrintWriter(sw);
e.printStackTrace(pw);return sw.toString();}
}

// not sure if this function is useful or not
@Override
public void start(Stage stage) {
try{Application.launch();
net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
stage.setTitle("Date pick test");//scn.getChildren().add(obj);
stage.setScene(scn);
stage.show();
}catch(Exception e){}
}

来证明编译是完美的。

:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtest>javac -cp ;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtest*;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtest *.java
D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtest>

好吧,那有什么问题吗?应该行得通吧?遗憾的是没有。下图是堆栈跟踪。显示类未找到

有人有类似的经验或解决方案吗?

edit1,好的,所以有人问Java版本,我不能评论。

D: 编码 JavaSourceCode 插件 userareaplugin otherpartylibderived time4jtest> java-version openjdk version "15"2020-09-15 OpenJDK运行环境(build 15+36-1562) OpenJDK 64位服务器虚拟机(build 15+36-1562,混合型)模式,共享)

jvm抱怨testdategui .类是的,这是第i类,应该有这个新阶段。注意,它实际上已经在执行第7行,但仍然抱怨没有找到类。@@

Thank you very much

edit2,基于亲爱的喷气机的建议。我试着确保不启动应用程序两次。删除了发射垃圾。2,确保它实际上是使用javafx线程javafx.application. platform . runlater

Walah,猜猜发生了什么?它实际上返回了"ok"。但是,ui不存在。(呜咽)

new function state>

public String plugstart(){
try{
javafx.application.Platform.runLater(new Runnable(){public void run(){
net.time4j.ui.javafx.CalendarPicker obj=net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults();
javafx.scene.Scene scn=new javafx.scene.Scene(obj, 300, 250);
Stage stage = new Stage();
stage.setTitle("Date pick test");//scn.getChildren().add(obj);
stage.setScene(scn);
stage.show();
}});
return "ok";}catch(Exception e){java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw =new java.io.PrintWriter(sw);
e.printStackTrace(pw);return sw.toString();}
}

好的,我尝试了这个解决方案并适应了它。我认为它应该工作,但我有classnotfound异常。

我认为这是我自己的类路径设置问题,而不是回答问题,所以我将接受ans。

对于那些好奇的人,现在我有这个新的错误。如果我使用"全新的类"

:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide>java -cp %newcp%--module-path %newcp% --add-modules=all-modules Testdategui
Error: Could not find or load main class ;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide*;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide
Caused by: java.lang.ClassNotFoundException: ;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide*;D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide
D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide>

引用src,应该工作吗?但我这边有问题

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
public class Testdategui extends Application 
{

@Override
public void start(Stage primaryStage) throws Exception {
// Launch all your JavaFX code and methods from inside this Entry Point function.
Stage stage = new Stage();
Scene scene = new Scene(net.time4j.ui.javafx.CalendarPicker.gregorianWithSystemDefaults());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}

}

edit 4,对不起,模块路径中没有空间,但出现了新的错误。我将尝试自己搜索答案

java -cp %newcp%——module-path %newcp%——add-modules=all-modules Testdategui启动层初始化时发生错误java. io.file. invalidpatheexception: Illegal char <*>在索引104:D:CodingJavaSourceCodepluginuserareapluginotherpartylibderivedtime4jtesttest3basedonSO_menoguide*

最终编辑,工作。java -cp %newcp%——module-path %cd%——add-modules ALL-MODULE-PATH Demo

Thannk你。

首先,您应该尝试创建一个简单的示例来查找问题所在。下次请考虑创建一个可复制的示例,以便社区可以更好地帮助(调试)。

"start()";方法在JavaFX应用程序线程上调用,当系统准备好应用程序开始运行时。你不能在这个线程之外做JavaFX的事情,这可能就是你崩溃的原因。我读到你不确定是否"开始"。是有用的,但出于这个原因,它是绝对必要的。

从哪里启动plugstart方法?如果它是在JavaFX应用程序线程之外启动的(所以目前在override之外)" start()">

这是一个可复制的JavaFX应用程序示例。

package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Launch all your JavaFX code and methods from inside this Entry Point function.
Stage stage = new Stage();
Scene scene = new Scene(new Label("I am a label object"));
stage.setScene(scene);
stage.show();
}

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

注意JavaFX代码是从"start"方法(所以舞台和场景是在那里创建的,等等),而不是从你的"main";方法。你只需要调用"launch(args)"当您想要启动JavaFX应用程序时,从main方法中调用一次。

你说你已经启动了应用程序,大概是从main方法启动的。为什么要同时在"开始"one_answers"启动"中启动它?和";plugstart"方法?这肯定会导致崩溃,特别是当"启动"时它已经在运行了。方法。

最新更新