Javafx Java FX设置边距
这个简单的示例创建了一个区域,其中两个矩形区域用红色标记。 我想使用Javafx Java FX设置边距,javafx,layout,margin,vbox,hbox,Javafx,Layout,Margin,Vbox,Hbox,这个简单的示例创建了一个区域,其中两个矩形区域用红色标记。 我想使用VBoxmargin方法将右侧区域向右推n个像素,但什么也没发生。为什么在这个例子中保证金不起作用?但它在场景生成器中工作 public class LayoutContainerTest extends Application { @Override public void start(Stage primaryStage) { VBox areaLeft = new VBox();
VBox
margin方法将右侧区域向右推n个像素,但什么也没发生。为什么在这个例子中保证金不起作用?但它在场景生成器中工作
public class LayoutContainerTest extends Application {
@Override
public void start(Stage primaryStage) {
VBox areaLeft = new VBox();
areaLeft.setStyle("-fx-background-color: red;");
areaLeft.setPrefSize(100, 200);
VBox areaMiddle = new VBox();
areaMiddle.setStyle("-fx-background-color: red;");
areaMiddle.setPrefSize(100, 200);
VBox areaRight = new VBox();
areaRight.setStyle("-fx-background-color: red;");
areaRight.setPrefSize(100, 200);
VBox.setMargin(areaRight, new Insets(0,0,0,50));
HBox root = new HBox(areaLeft,areaMiddle,areaRight);
root.setSpacing(30);
Scene scene = new Scene(root, 600, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
您正在使用
VBox.setMargin()
,但应改用HBox
方法:
HBox.setMargin(areaRight, new Insets(0, 0, 0, 50));
原因是,您正在为VBox
的子项设置边距,而areaRight
是HBox
的子项。如果您使用代码,然后将areaRight
放入VBox
,您将看到预期的边距
您提到它在SceneBuilder中工作,但是如果您检查实际的FXML代码,您将看到SceneBuilder正确地使用了HBox
:
<VBox fx:id="areaRight" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: red;">
<HBox.margin>
<Insets left="50.0" />
</HBox.margin>
</VBox>
哇,太快了!非常感谢。现在完全清楚了。