我在 Netbeans 7.3.1 中使用 JavaFX 时遇到了一些麻烦。我正在尝试制作一个掷骰子的游戏。该程序应该询问用户骰子是否公平,以及其他信息(例如面数)以初始化骰子以滚动。单击滚动按钮时,它将滚动骰子。
我的问题是,由于 JavaFX 在启动 GUI 时会忽略主程序,因此我不确定在加载 GUI 之前在哪里(或如何)向用户询问此信息。
public class DiceGame extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Roll Die!");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//
// code for rolling a die goes here.
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Dice Game");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
//ask user for n value to initialize a die object before the javafx ui is launched.
System.out.println("Is your die loaded or fair? *Input 1 for fair and 2 for loaded* ");
Scanner fair_or_loaded = new Scanner(System.in); //Determines fair or loaded die.
int input = fair_or_loaded.nextInt();
if (input == 1) {
System.out.println("Input an N value for a fair die: ");
Scanner user_input = new Scanner(System.in); //Created a fair die.
int n = user_input.nextInt();
Die D1 = new Die(n); //User created die.
} else { //Loaded die is created. Need Scanner for loadedSide and LoadFactor.
System.out.println("Input an N value for a loaded die: ");
Scanner loaded_n_input = new Scanner(System.in);
int loaded_n = loaded_n_input.nextInt(); //loaded n value created
System.out.println("Input a side to be loaded: ");
Scanner user_side = new Scanner(System.in);
int side_loaded = user_side.nextInt(); //loaded side created
System.out.println("Input a load factor: ");
Scanner user_load = new Scanner(System.in);
int factor_load = user_load.nextInt(); //load factor created
LoadedDie DL = new LoadedDie(loaded_n, side_loaded, factor_load); //Loaded die is created.
}
launch(args);
}
}
首先十分感谢!