java - 如何在边框底部的每个角落放置两个按钮
问题描述
我正在尝试放置两个不同的按钮,一个在左下角,另一个在右下角。这两个按钮不同HBox
,两个 HBox 位于底部Borderpane
为此,我这样做了,它可以工作,但很难看:
envoisButton = new Button("Envoyer la commande");
envoisButton.setStyle("-fx-font: 20px \"Roboto\";");
envoisButton.setPrefSize(300,50);
envoisButton.setGraphic(new ImageView(new Image("PleinVide/images/OK.png")));
HBox.setMargin(envoisButton,new Insets(0,20,20,20));
HBox rightButtons = new HBox(envoisButton);
rightButtons.setAlignment(Pos.CENTER_RIGHT);
synchroButton = new Button("Synchroniser");
synchroButton.setStyle("-fx-font: 20px \"Roboto\";");
synchroButton.setPrefSize(300,50);
synchroButton.setGraphic(new ImageView(new Image("PleinVide/images/synchronize.png")));
HBox.setMargin(synchroButton,new Insets(0,20,20,20));
HBox leftButtons = new HBox(synchroButton);
leftButtons.setAlignment(Pos.CENTER_LEFT);
HBox buttons = new HBox(leftButtons,rightButtons);
buttons.setSpacing(primaryScreenBounds.getWidth()*0.65); //This is very ugly (primaryScreenBounds == my screen resolution)
borderPane.setBottom(buttons);
结果如下: 我们可以看到 2 个按钮在我想要的位置,但是如果将来我想添加另一个按钮,这将根本不起作用:/
您有解决方案将两个按钮放在角落吗?非常感谢。
解决方案
HBox
将两个es包裹在另一个中HBox
,设置适当的对齐方式,并让它们始终水平增长。
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.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane(new Label("Hello, World!"));
HBox leftBox = new HBox(new Button("Left Button"));
leftBox.setAlignment(Pos.CENTER_LEFT);
HBox.setHgrow(leftBox, Priority.ALWAYS);
HBox rightBox = new HBox(new Button("Right Button"));
rightBox.setAlignment(Pos.CENTER_RIGHT);
HBox.setHgrow(rightBox, Priority.ALWAYS);
HBox bottom = new HBox(leftBox, rightBox);
bottom.setPadding(new Insets(10));
root.setBottom(bottom);
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
}
另一种选择是使用单个HBox
并使用“填充”区域/窗格。例如:
HBox hbox = new HBox();
hbox.getChildren().add(new Button("Left Button"));
Pane filler = new Pane();
hbox.getChildren().add(filler);
HBox.setHgrow(filler, Priority.ALWAYS);
hbox.getChildren().add(new Button("Right Button"));
borderPane.setBottom(hbox);
推荐阅读
- angular - 使用 CSS Grid(Angular 6 with Angular Material)时,mat-raised-button 在 Safari 浏览器上不起作用
- python - 字典,使用 .get 添加多个值
- apache-spark - 我应该使用哪种转换来将映射减少到每行具有最小值的项目?
- java - 使用新的请求拦截器假装客户端重试器?
- google-sheets - 自动更改公式中的工作表参考
- c# - 在“使用”块处理使用的实例时中止线程
- javascript - 删除元素表单对象并重新索引并添加新元素
- php - 如何装饰学说收藏
- java - Elasticsearch - 如何对索引中的匹配项进行分组和计数
- three.js - 重叠的半透明对象未按预期呈现