我正在尝试放两个不同的按钮,一个在左下角,另一个在右下角.这两个按钮是不同的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);
Run Code Online (Sandbox Code Playgroud)
这是结果:
我们可以看到2个按钮是我想要它们的位置,但如果将来我想添加另一个按钮,这根本不起作用:/
你有解决方案将两个按钮放在角落吗?非常感谢你.
将两个HBoxes 包裹在另一个中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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
364 次 |
| 最近记录: |