尝试将 HBox 分配给底部时 JavaFX BorderPane 抛出错误

标签 java javafx hbox borderpane

我试图将按钮放在 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 后我也很难理解它。

任何帮助将不胜感激。

最佳答案

您无法将节点添加到另一个 Pane ,将其设为场景的根。 (阅读 stack trace:它准确地告诉您出了什么问题。)

我想你的意思是

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

而不是

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

关于尝试将 HBox 分配给底部时 JavaFX BorderPane 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42774811/

相关文章:

java 如何获取数组中的红色值

java - Apache Shiro + 身份验证问题

java - HttpClient : ssl handshake on every request

java - 总是使用 get 和 set 方法来访问类自己的成员字段是否属于反模式?

listview - 每个单元格中带有按钮的 javafx listview

java - 将 Canvas 转换为 WritableImage 的替代方法

java - 如何使按钮事件将输出写入文本字段

java - 对象包含另一个对象。如何在TableView中显示呢?

java - 如何在javafx中将父AnchorPane与HBox匹配

arrays - LaTeX 有数组数据结构吗?