使用另一个类中的JavaFx应用程序实例
我有一个如下所示的MainWindowFx类。它基本上创建了一个简单的使用另一个类中的JavaFx应用程序实例,javafx,Javafx,我有一个如下所示的MainWindowFx类。它基本上创建了一个简单的JavaFXGUI package drawappfx; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.TextArea; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.sce
JavaFX
GUI
package drawappfx;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.control.TextAreaBuilder;
/**
*
* @author Hieu
*/
public class MainWindowFX extends Application{
public static final int DEFAULT_WIDTH = 600;
public static final int DEFAULT_HEIGHT = 600;
private int width;
private int height;
private Scene scene;
private TextArea messageView;
private Button quitButton;
private BorderPane layout;
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
System.out.println("Started building GUI....");
this.buildGUI();
System.out.println("Finished building GUI");
this.primaryStage = primaryStage;
primaryStage.setTitle("Hello World!");
primaryStage.setScene(this.scene);
primaryStage.show();
System.out.println("Where the hell are you?");
}
public Scene getScene() {
return this.scene;
}
public BorderPane getBorderPane() {
return this.layout;
}
public Stage getPrimaryStage() {
return this.primaryStage;
}
public void buildGUI() {
System.out.println("Before layout");
this.layout = new BorderPane();
System.out.println("Before vbox");
this.layout.setBottom(this.addVBox());
System.out.println("before new scene");
this.scene = new Scene(this.layout, DEFAULT_WIDTH, DEFAULT_HEIGHT);
System.out.println("after new scene");
}
public VBox addVBox() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(15, 12, 15, 12));
// message box
this.messageView = TextAreaBuilder.create()
.prefRowCount(5)
.editable(false)
.build();
// quit button
this.quitButton = new Button("Quit");
this.quitButton.setPrefSize(100, 20);
System.out.println("think of a good message?");
vbox.getChildren().addAll(this.messageView, this.quitButton);
System.out.println("before returning vbox");
return vbox;
}
public void postMessage(final String s) {
this.messageView.appendText(s);
}
}
现在我想在另一个类中使用此对象的实例:
package drawappfx;
import java.io.InputStreamReader;
import java.io.Reader;
import javafx.scene.layout.BorderPane;
public class DrawAppFx
{
public static void main(String[] args)
{
final MainWindowFX main = new MainWindowFX();
BorderPane layout = main.getBorderPane();
Reader reader = new InputStreamReader(System.in);
Parser parser = new Parser(reader,layout,main);
main.start(main.getPrimaryStage());
parser.parse();
}
}
但当我运行此命令时,我遇到了以下错误:
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:658)
at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
at javafx.scene.Scene.<init>(Scene.java:287)
at javafx.scene.Scene.<init>(Scene.java:226)
at drawappfx.MainWindowFX.buildGUI(MainWindowFX.java:74)
at drawappfx.MainWindowFX.start(MainWindowFX.java:47)
at drawappfx.DrawAppFx.main(DrawAppFx.java:39)
... 6 more
Java Result: 1
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:601)
位于com.javafx.main.main.launchApp(main.java:658)
位于com.javafx.main.main.main(main.java:805)
原因:java.lang.IllegalStateException:不在FX应用程序线程上;currentThread=main
位于com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
位于com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
在javafx.scene.scene.(scene.java:287)
在javafx.scene.scene.(scene.java:226)
位于drawappfx.MainWindowFX.buildGUI(MainWindowFX.java:74)
在drawappfx.MainWindowFX.start(MainWindowFX.java:47)
位于drawappfx.drawappfx.main(drawappfx.java:39)
... 还有6个
Java结果:1
我对此做了一些搜索,并猜测它与线程有关。。。但我还是不知道。有什么建议吗?我已经多次遇到这个问题,有一个相当简单的方法来解决它 首先,让我向您介绍,基本上您希望创建一个与所有GUI类有关系的类 (也就是说,不同的GUI类之间没有各自的实例,而所有GUI类都对中介具有相同的引用) 这是你问题的旁白 要更改窗口,您需要通过
阶段
,新窗口应放在该阶段,因此您的代码只需稍作更改:
现在我不经常这样做,但在您的情况下,我会做一个例外,以下代码由一个类组成,您可以“复制粘贴”到您的程序中,并使用它来解决问题。在代码之后,我将确切地解释我所做的:
调解人
public class Mediator extends Application {
private DrawAppFx daf;
private MainWindowFX mainWindow;
private Stage primaryStage;
public Mediator(){
daf = new DrawAppFx(this);
mainWindow = new MainWindowFx(this);
}
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
mainWindow.start(primaryStage);
}
public void changeToDaf(){
daf.start(primaryStage);
}
}
现在,每个DrawAppFx
和MainWindowFx
都必须有一个传递中介对象的构造函数,因此与中介具有“Has-a”关系
这背后的原因是中介模式现在处于控制之中,如果您创建更多的窗口,那么很容易实现,只需将它们添加到中介中,并添加一个中介可以更改为该窗口的方法即可