Java 如何使用hbox向StackPane添加填充

Java 如何使用hbox向StackPane添加填充,java,javafx,Java,Javafx,我还在学习如何处理面板和hbox、vbox等。。 我希望能够设置围绕StackPane中心的填充物 @Override public void start(Stage stage) throws Exception { stage.setTitle("GUIGUI"); HBox hbox = new HBox(10); // number sets spacing between things

我还在学习如何处理面板和hbox、vbox等。。 我希望能够设置围绕StackPane中心的填充物

@Override
    public void start(Stage stage) throws Exception {

        stage.setTitle("GUIGUI");
       
        HBox hbox = new HBox(10); // number sets spacing between things
        
        Button roll1 = new Button("Roll");
        hbox.setPadding(new Insets(40,40,40,40));
        hbox.getChildren().add(roll1);  
        
        StackPane root = new StackPane();
        root.setAlignment(hbox, Pos.CENTER);
        root.getChildren().add(hbox);

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();  
        
    }
public static void main(String[] args) {
        launch(args);
         
    }
...
我已经尝试使用hbox在roll(这是一个按钮)周围设置填充,以便在StackPane中调用(?)时,它的填充为40,40,40,40

@Override
    public void start(Stage stage) throws Exception {

        stage.setTitle("GUIGUI");
       
        HBox hbox = new HBox(10); // number sets spacing between things
        
        Button roll1 = new Button("Roll");
        hbox.setPadding(new Insets(40,40,40,40));
        hbox.getChildren().add(roll1);  
        
        StackPane root = new StackPane();
        root.setAlignment(hbox, Pos.CENTER);
        root.getChildren().add(hbox);

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();  
        
    }
public static void main(String[] args) {
        launch(args);
         
    }
...
我的代码在按钮周围设置了填充,但它并没有位于中间,即使我已经这样做了

root.setAlignment(hbox, Pos.CENTER);

你可能想要这样的东西。这会将填充添加到堆栈窗格并使按钮居中。 确保只有javafx导入,而没有awt导入

    @Override
    public void start(Stage stage) throws Exception {

        stage.setTitle("GUIGUI");
       
        HBox hbox = new HBox(10); // number sets spacing between things
        
        Button roll1 = new Button("Roll");
        hbox.setPadding(new Insets(40,40,40,40));
        hbox.getChildren().add(roll1);
        hbox.setAlignment(Pos.CENTER);
        hbox.setStyle("-fx-background-color:TAN");
        
        StackPane root = new StackPane();
        root.getChildren().add(hbox);
        root.setPadding(new Insets(40,40,40,40));
        root.setStyle("-fx-background-color:BLUE");
        

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();  
        
    }

我不能100%确定您想做什么,但我认为您应该给
HBox
一个首选尺寸。然后您应该将
HBox
max size设置为
USE\u PREF\u size

我用@Kleopatra的建议更新了答案。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class App extends Application
{

    @Override
    public void start(Stage stage)
    {
        stage.setTitle("GUIGUI");

        HBox hbox = new HBox(10); // number sets spacing between things
        hbox.setStyle("-fx-background-color: yellow");
        Button roll1 = new Button("Roll");
        hbox.setPadding(new Insets(40, 40, 40, 40));
        hbox.getChildren().add(roll1);
        hbox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
        StackPane root = new StackPane();
        root.getChildren().add(hbox);
        root.setStyle("-fx-background-color: green");
        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();
    }
}

请..我看不到hbox.setPadding(新插图(40,40,40,40));,但是它在那里正确吗?是的。要查看此填充的效果,请删除行hbox.setAlignment(Pos.CENTER);并移除HBox的填充物。运行应用程序。roll1应该位于HBox的左上角。现在将HBox的填充添加回(但不要将对齐线放回),并运行应用程序。您现在应该看到roll1是一个不同的位置(但仍然在左上角)。。否-硬编码大小总是错误;)而且你也不需要它:对于Stackpane,它足以限制hbox的最大值(正如你通过use_pref正确地做的那样)并添加一个填充(如问题中所述),而中心对齐是默认设置:)只是在挑剔的心情下,在11月的一个灰色星期天。@kleopatra,谢谢!根据你的建议,我最终将成为
JavaFX
的大师。