我目前正在研究一个javaFX项目,该项目必须处理彼此堆叠3个窗格的问题。顶部的一个包含5个单选按钮,中间的文本显示为“编程很有趣”,底部的窗格包含两个带有“ <=”符号的按钮,另一个包含“ =>”符号。本质上,我已经完成了该程序,但是在底部按钮居中时遇到了问题。我对单选按钮使用了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();
}
}
答案 0 :(得分:1)
将bottomPane从Pane更改为StackPane并删除填充。还要将bottomButtons的对齐方式设置为Pos.CENTER。
bottomButtons.setAlignment(Pos.CENTER);
StackPane bottomPane = new StackPane();
//bottomButtons.setPadding(new Insets(0,0,0,225));
Pane没有默认的布局策略。而StackPane具有布局策略,默认情况下它与中心对齐。