【发布时间】:2018-07-01 07:06:35
【问题描述】:
我尝试简单的测试
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new BorderPane();
pane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
HBox hBox = new HBox();
hBox.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
hBox.setPrefHeight(100);
hBox.setPrefWidth(100);
pane.getChildren().add(hBox);
primaryStage.setScene(new Scene(pane, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
期望在红色窗格上看到绿色方形 HBox,但只看到红色窗格。
但是如果我直接使用 HBox 作为根视图(将它传递给new Scene()),绿色框是可见的。
为什么在 Pane 中缺少 HBox?
【问题讨论】:
-
您是否尝试在 HBox 中添加一些内容,例如: Button button1 = new Button("Button Number 1");按钮 button2 = new Button("按钮编号 2"); HBox hbox = new HBox(button1, button2);
标签: java user-interface javafx javafx-8