问题描述
我无法在Java代码的VBox中将Button居中!我使用Scene Builder创建了一个SplitPane,左侧的AnchorPane的按钮位于VBox的中央.我想在Java代码中的右侧AnchorPane中而不是FXML中的VBox中重新创建一个Button.但是,尽管我使用了vb.setAlignment(Pos.CENTER);
,但右按钮并未居中:
I fail to center a Button in a VBox in Java code!I used Scene Builder to create a SplitPane with the left AnchorPane having a Button centered in a VBox.I want to recreate this, a Button in a VBox, in the right AnchorPane but not in FXML, in Java Code. But the right Button does not center, although I use vb.setAlignment(Pos.CENTER);
:
我的FXML代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testimageview.MainViewController">
<children>
<SplitPane dividerPositions="0.5" prefHeight="200.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane fx:id="leftAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<VBox alignment="CENTER" prefHeight="198.0" prefWidth="171.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button mnemonicParsing="false" text="FXML" />
</children>
</VBox>
</children>
</AnchorPane>
<AnchorPane fx:id="rightAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
还有我的Java Controller类代码:
And my Java Controller class code:
package testimageview;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
public class MainViewController implements Initializable {
@FXML
private AnchorPane leftAnchorPane;
@FXML
private AnchorPane rightAnchorPane;
public MainViewController() {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
VBox.setVgrow(leftAnchorPane, Priority.ALWAYS);
VBox vb = new VBox();
Button rightButton = new Button();
rightButton.setText("Java");
vb.setAlignment(Pos.CENTER);
vb.getChildren().addAll(rightButton);
rightAnchorPane.getChildren().add(vb);
}
}
推荐答案
好吧,您没有定义VBox的尺寸,因此默认情况下(在ScrollPane内)它会适合您的孩子的大小,就像您的Button大小一样50,50之类的东西,所以您看不到对齐方式.您需要做的就是定义VBox的大小以匹配第二个AnchorPane的大小,或者您可以像这样绑定它们的尺寸(Width,height):
Well you did not define the dimensions for the VBox so by default (inside a ScrollPane) it will fit the size of it's children in your case a Button size like 50,50 or something like that, so you can not see the alignment. All you need to do is to define the size of the VBox to match the size of the second AnchorPane or you can just bind their dimensions ( Width, height ) like :
vb.prefWidthProperty().bind(rightAnchorPane.widthProperty());
vb.prefHeightProperty().bind(rightAnchorPane.heightProperty());
这篇关于VBox中的JavaFX中心按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!