【发布时间】:2016-07-04 10:04:44
【问题描述】:
这是我使用 Swing 时的代码的 sn-p:
private static Gui gui;
public static void main(String[] args){
gui = new Gui(); //(1)
gui.menu(); //(2)
gui.show(); //(3)
}
- 构造函数初始化
JFrame - 创建
JLabel和JPanel并将它们都添加到框架中。 - 将框架可见性设置为
true。
我一直在尝试,但无法使用 Java-FX 获得我想要的结果。这是我最后一次尝试的样子:
private static Gui gui;
public static void main(String[] args){
gui = new Gui();
Application.launch(Gui.class, args);
gui.menu();
gui.show();
}
GUI类:
private static Stage stage;
public class Gui extends Application {
publc void start(Stage primaryStage){
Gui.stage = primaryStage;
stage.setTitle("title");
//I read that some method blocking is involved here, I want to go back!
}
public void menu(){
BorderPane abc = new BorderPane();
Scene scene = new Scene(abc, 200, 200);
stage.setScene(scene);
//if I somehow mange to reach this part, there will be some null pointer
//exception, well everything looks real initialised to me
}
public void show(){
stage.show();
//I have never reach this part
}
}
【问题讨论】:
-
顺便说一句。 Swing 代码是错误的,因为它无法在 EDT 上启动 GUI。代码的另一个问题是它将
gui成员声明为static。 不要尝试在任何其他 GUI 工具包中复制它。而是从Getting Started with JavaFX 开始并学习如何正确使用 GUI 工具包(您应该也使用 Swing 完成一些事情)。 -
我投票决定将此问题作为题外话结束,因为提问者应该以Getting Started with JavaFX开头。
-
别着急,我终于找到了正确的做法。这出乎意料地容易。无论如何,也许我应该删除这个