gpt4 book ai didi

java - HBox.setAlignments(Pos.CENTER) 不适用于我的 JavaFX 程序中的 HBox 之一。

转载 作者:行者123 更新时间:2023-11-30 05:53:08 26 4
gpt4 key购买 nike

我目前正在开发一个 javaFX 项目,该项目必须处理将 3 个 Pane 堆叠在一起的问题。顶部一个包含 5 个单选按钮,中间包含一个文本“编程很有趣”,底部 Pane 包含两个带有“<=”符号的按钮,另一个包含“=>”符号。基本上,我已经完成了该程序,但我在底部按钮居中时遇到了问题。我使用 RadioButtons.setAlignment(Pos.CENTER) 作为单选按钮,效果很好,但是,由于某种原因,当我键入 BottomButtons.setAlignment(Pos.CENTER) 时它不起作用。我将不胜感激任何帮助。

HBox bottomButtons = new HBox(5);
Button leftb = new Button("<=");
Button rightb = new Button("=>");
bottomButtons.getChildren().addAll(leftb, rightb);

Pane bottomPane = new Pane();
bottomButtons.setAlignment(Pos.CENTER);
bottomPane.getChildren().addAll(bottomButtons);
pane.setBottom(bottomPane);

这是完整的代码,以防我遗漏其中的内容。

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.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Unit08_Prog01 extends Application {
public static void main(String[] args) {
launch(args);
}
Text text = new Text(40,40,"Programming is fun");
public BorderPane getPane() {
text.setFont(new Font(20));
HBox radioButtons = new HBox(5);
RadioButton redrb = new RadioButton("Red");
RadioButton yellowrb = new RadioButton("Yellow");
RadioButton whiterb = new RadioButton("White");
RadioButton orangerb = new RadioButton("Orange");
RadioButton greenrb = new RadioButton("Green");
radioButtons.getChildren().addAll(redrb, yellowrb, whiterb, orangerb, greenrb);
radioButtons.setAlignment(Pos.CENTER);

BorderPane pane = new BorderPane();
pane.setTop(radioButtons);
ToggleGroup group = new ToggleGroup();
redrb.setToggleGroup(group);
yellowrb.setToggleGroup(group);
whiterb.setToggleGroup(group);
orangerb.setToggleGroup(group);
greenrb.setToggleGroup(group);



Pane centerPane = new Pane();
centerPane.getChildren().add(text);
pane.setCenter(centerPane);
centerPane.setStyle("-fx-border-color: black");

HBox bottomButtons = new HBox(5);
Button leftb = new Button("<=");
Button rightb = new Button("=>");
bottomButtons.getChildren().addAll(leftb, rightb);

Pane bottomPane = new Pane();
bottomButtons.setPadding(new Insets(0,0,0,225));
bottomPane.getChildren().addAll(bottomButtons);
pane.setBottom(bottomPane);

whiterb.setSelected(true);
redrb.setOnAction(e -> {
if (redrb.isSelected()) {
centerPane.setStyle("-fx-background-color: red");
}
});
yellowrb.setOnAction(e -> {
if (yellowrb.isSelected()) {
centerPane.setStyle("-fx-background-color: yellow");
}
});
whiterb.setOnAction(e -> {
if (whiterb.isSelected()) {
centerPane.setStyle("-fx-background-color: white");
}
});
orangerb.setOnAction(e -> {
if (orangerb.isSelected()) {
centerPane.setStyle("-fx-background-color: orange");
}
});
greenrb.setOnAction(e -> {
if (greenrb.isSelected()) {
centerPane.setStyle("-fx-background-color: green");
}
});

leftb.setOnAction(e -> {
text.setX(text.getX() - 10 );
});
rightb.setOnAction(e -> {
text.setX(text.getX() + 10);
});

return pane;
}

@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Scene scene = new Scene(getPane(),500,200);
primaryStage.setTitle("Unit08_Prog1");
primaryStage.setScene(scene);
primaryStage.show();



}

}

最佳答案

将底部 Pane 从 Pane 更改为堆栈 Pane 并删除填充。还将 BottomButtons 的对齐方式设置为 Pos.CENTER。

bottomButtons.setAlignment(Pos.CENTER);
StackPane bottomPane = new StackPane();
//bottomButtons.setPadding(new Insets(0,0,0,225));

Pane 没有默认布局策略。而 StackPane 有布局策略,默认情况下它居中对齐。

关于java - HBox.setAlignments(Pos.CENTER) 不适用于我的 JavaFX 程序中的 HBox 之一。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com