JavaFX中场景宽高、HBox节点间距等属性的单位是什么?
JavaFX中场景与布局属性的单位说明
在JavaFX里,设置Scene宽高、HBox节点间距这类尺寸属性时,默认单位是逻辑像素(Logical Pixels)。这是一种设备无关的单位,会根据屏幕DPI自动适配,保证UI在不同分辨率设备上的视觉大小一致。
针对你提供的示例代码:
new HBox(15, btn1, btn2)中的15:代表HBox容器内按钮之间的间距,单位为逻辑像素new Scene(root,250,50)中的250和50:分别代表Scene的宽度和高度,单位同样是逻辑像素
示例代码:
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class TestGUI extends Application{ @Override public void start(Stage primaryStage) { Button btn1 = new Button("OK"); Button btn2 = new Button("Cancel"); HBox root = new HBox(15,btn1,btn2); root.setAlignment(Pos.CENTER); Scene scene = new Scene(root,250,50); primaryStage.setScene(scene); primaryStage.setTitle("GUI Example"); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
内容的提问来源于stack exchange,提问作者Osama El-Ghonimy
相关产品推荐
相关产品推荐

