简体   繁体   English

如何在边框底部的每个角落放置两个按钮

[英]How to put two buttons in each corner in the bottom of a borderpane

I'm trying to put two different buttons, one at the bottom left corner and another one to the bottom right corner.我试图放置两个不同的按钮,一个在左下角,另一个在右下角。 These two buttons are in differents HBox and the two HBoxes are in the bottom of the Borderpane这两个按钮是在型动物HBox和两个横向方框都在底部Borderpane

To do that I did this, it works but it is ugly:为此,我这样做了,它可以工作,但很难看:

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);

Here's the result:结果如下: 预期的按钮位置 We can see that the 2 buttons are where I want them to be, but if in the future I want to add another button, this will not work at all :/我们可以看到这 2 个按钮是我想要的位置,但是如果将来我想添加另一个按钮,这根本不起作用:/

Do you have a solution to place the two buttons at the corners?你有没有办法把两个按钮放在角落里? Thank you very much.非常感谢。

Wrap the two HBox es in another HBox , set the appropriate alignments, and have them both always grow horizontally.将两个HBox包裹在另一个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();
  }

}

Another option is to use a single HBox and use a "filler" region/pane.另一种选择是使用单个HBox并使用“填充”区域/窗格。 For example:例如:

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);

I would suggest我会建议

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <Pane style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
   </center>
   <bottom>
      <AnchorPane maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: green;" BorderPane.alignment="CENTER">
         <children>
            <Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="20.0" />
            <Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
         </children>
      </AnchorPane>
   </bottom>
</BorderPane>

you could use a gridpane.你可以使用网格板。

Either instead of adding buttons in your example, add a gridpane, and add the buttons in that gridpane.要么在您的示例中添加按钮,要么添加一个网格窗格,然后在该网格窗格中添加按钮。

Or或者

Use a gridpane as your scene and style and add constraints accordingly (this is more difficult).使用网格窗格作为您的场景和样式并相应地添加约束(这更困难)。 This way if you want to add buttons, all you need to do is figure out the coordinates for the slots in the gridpane.这样,如果您想添加按钮,您需要做的就是找出网格窗格中插槽的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM