我正在尝试创建一个面向对象的JavaFX程序,但是我遇到了一个我似乎无法解决的问题,基本上Window变量都设置好了,当我将它们打印到控制台时它们都被设置了但是当我在启动方法中使用它们时,它似乎并不承认它们在那里。
主要课程:
import com.mersey.ui.Window;
public class Main
{
public static void main(String[] args)
{
Window win = new Window();
win.Create("Mersey", 980, 640, false);
Window.launch(Window.class, args);
}
}
窗口类:
package com.mersey.ui;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Window extends Application
{
protected Stage WindowStage;
protected Pane WindowRoot;
protected Scene WindowScene;
protected String WindowTitle;
protected int WindowWidth;
protected int WindowHeight;
protected boolean WindowResizable;
public void Create(String title, int width, int height, boolean resizable)
{
WindowTitle = title;
WindowWidth = width;
WindowHeight = height;
WindowResizable = resizable;
/*
System.out.print(WindowTitle+"\n");
System.out.print(WindowWidth+"\n");
System.out.print(WindowHeight+"\n");
System.out.print(WindowResizable);*/
}
@Override
public void start(Stage stage) throws Exception
{
WindowStage = stage;
WindowRoot = new Pane();
WindowScene = new Scene(WindowRoot, WindowWidth, WindowHeight);
WindowStage.setScene(WindowScene);
WindowStage.setTitle(WindowTitle);
WindowStage.setWidth(WindowWidth);
WindowStage.setHeight(WindowHeight);
WindowStage.setMinWidth(WindowWidth);
WindowStage.setMinHeight(WindowHeight);
WindowStage.setResizable(WindowResizable);
WindowStage.show();
}
}
答案 0 :(得分:0)
我认为*你想要在这里做什么,不是构造Window对象然后以你现在的方式调用Window.launch,而是通过String汇集宽度高度的值和所有这些值[ args传入Window.launch。文档说你可以通过getParameters()来检索这些值,所以它看起来像这样:
//in main
String[] launchArgs = new String[] {"Width=800", "Height=600"};
Window.launch(Window.class, launchArgs);
..........................
//in start(Stage)
String[] args = getParameters();
for (String s : args) {
if (s.contains("Width")) {
//parse and set the window width here
}
}
请参阅:http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#launch(java.lang.Class,java.lang.String ...)
当然,这假设您希望能够从main中为这些类字段提供任意值。如果没有,我只需硬编码默认值,或者从start(Stage)方法中的配置中提取它们。