【问题标题】:JavaFX BorderPane throwing errors when trying to assign HBox to the bottom尝试将 HBox 分配到底部时,JavaFX BorderPane 抛出错误
【发布时间】:2023-04-04 19:57:01
【问题描述】:

我试图将按钮放在我的 BorderPane 的底部,但我一直收到错误,我以前可以使用它,但现在收到错误“应用程序启动方法中的异常”。

package assign3;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Question2 extends Application
{

    @Override
    public void start( Stage obPrimeStage ) throws Exception
    {
        Button btRed = new Button("Red");
        Button btGreen = new Button("Green");
        Button btBlue = new Button("Blue");
        Button btOrange = new Button("Orange");
        Button btStart = new Button("Start");

        BorderPane obBorder = new BorderPane();
        HBox obPane = new HBox();

        obPane.getChildren().add(btRed);
        obPane.getChildren().add(btGreen);
        obPane.getChildren().add(btBlue);
        obPane.getChildren().add(btOrange);
        obPane.getChildren().add(btStart);

        obBorder.setBottom(obPane);

        Scene obScene = new Scene(obPane, 400, 400);

        obPrimeStage.setTitle("Question 2");
        obPrimeStage.setScene(obScene);
        obPrimeStage.show();

        btRed.setOnAction((ActionEvent e) -> 
        {
            obPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
        });

        btGreen.setOnAction((ActionEvent e) -> 
        {
            obPane.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
        });

        btBlue.setOnAction((ActionEvent e) -> 
        {
            obPane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
        });



    }

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

    }

}

我觉得它与 obBorder.setBottom(obPane) 有关,但不确定。我们的讲师对所有这些都进行了掩饰,即使在查看了 javadoc 之后,我也很难理解它。

任何帮助将不胜感激。

【问题讨论】:

  • 旁白:为什么几乎所有变量名都以ob 开头?
  • 我的导师喜欢为所有变量使用前缀。他使用 ob 表示对象的实例。
  • 这毫无意义:Java 中几乎所有东西都是对象。
  • 你应该知道这是完全不标准的。我强烈主张尽快改掉这个习惯。
  • 您的SceneobScene?这是 NSFW 吗?

标签: java javafx hbox borderpane


【解决方案1】:

您不能将节点添加到另一个窗格,并且使其成为场景的根。 (阅读stack trace:它准确地告诉你出了什么问题。)

我想你是说

Scene obScene = new Scene(obBorder, 400, 400);

而不是

Scene obScene = new Scene(obPane, 400, 400);

【讨论】:

  • 哦..我的上帝。这是一个漫长的一天。谢谢!我觉得我的眼睛需要休息一下。
  • 再说一次,任何人都可能犯这种错误。这就是为什么堆栈跟踪包含告诉您您做错了什么的信息。阅读它。
  • 是的,可以。以前没有看过,所以我只看到许多红线并开始恐慌。现在通过它更有意义。
  • 我添加了一个指向答案的链接,其中包含关于如何从堆栈跟踪中读取和诊断问题的非常好的描述。可能是对 SO 最有帮助的东西 :)。
  • 你是一个真正的英雄。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-10-31
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
相关资源
最近更新 更多