简体   繁体   中英

Netbeans not executing JavaFX application properly

I am starting up with javaFX application on Netbeans and trying sample codes. Problem I am facing is that when I run the code from Netbeans the application runs successfully but buttons are not arranged properly. But when I run it from the jar file from binary folder it works fine. is there any fix for that in Netbeans ?

here is the code I am using:

package guiexperiments;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class GUIExperiments extends Application {

@Override
public void start(Stage primaryStage) {
    Button btOK = new Button("OK");
    Scene scene = new Scene(btOK, 300, 350); 
    primaryStage.setTitle("MyJavaFX"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage


    Stage stage = new Stage(); // Create a new stage 
    stage.setTitle("Second Stage"); // Set the stage title
    // Set a scene with a button in the stage
    stage.setScene(new Scene(new Button("New Stage"), 300, 300)); stage.show();
    stage.setResizable(false);

}

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

}

Sorry can't post images as I need 10 reputation points to post images ... urgh.

Its because you are not using any layouts

Try this code and you will understand

package Example;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Example extends Application {

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Click Me");

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Title");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM