【问题标题】:Netbeans not executing JavaFX application properlyNetbeans 未正确执行 JavaFX 应用程序
【发布时间】:2015-11-10 23:42:12
【问题描述】:

我开始在 Netbeans 上使用 javaFX 应用程序并尝试示例代码。我面临的问题是,当我从 Netbeans 运行代码时,应用程序运行成功,但按钮排列不正确。但是当我从二进制文件夹中的 jar 文件运行它时,它工作正常。 Netbeans 中有什么解决方法吗?

这是我正在使用的代码:

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);
}

}

抱歉,我无法发布图片,因为我需要 10 个声望点才能发布图片……呃。

【问题讨论】:

  • 您使用的是什么版本的网络豆? Java 版本?
  • 添加您要上传的图片链接,我们中的某个人会为您完成...

标签: java button layout netbeans javafx


【解决方案1】:

因为你没有使用任何布局

试试这段代码,你就会明白

包示例;

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);
  }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2020-06-19
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多