我要做的是将HBox按钮与“底部位置”对话框的中心对齐。我想在fxml中执行此操作。然而,边框平面对齐在标签中有效。这是我这边的代码。我认为BorderPane.lalignment=“BOTTOM_CENTER”必须工作,即使标记是BOTTOM。
类文件:
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HBoxDialog extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("HBoxDialog.fxml"));
primaryStage.setScene(new Scene(root, 500, 100));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="this is dialogbox" BorderPane.alignment="TOP_CENTER"/>
<font>
<Font size="35"/>
</font>
</top>
<bottom>
<HBox spacing="10">
<Button text="Okay" prefWidth="90" BorderPane.alignment="BOTTOM_CENTER"/>
<Button text="Cancel" prefWidth="90" BorderPane.alignment="BOTTOM_CENTER"/>
<Button text="Help" prefWidth="90" BorderPane.alignment="BASELINE_RIGHT"/>
</HBox>
</bottom>
</BorderPane>
BorderPlane.alignment
静态属性仅适用于父级为BouderPlane
的节点。FXML文件中定义的Button
有一个HBox
作为父级,因此在按钮上设置BorderPane.agnment
属性将无效。
您可以通过使用HBox
的alignment
属性(将HBox
的子节点定位在其边界内),将HBox
HBox内的按钮居中,来实现所需的效果:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="this is dialogbox"
BorderPane.alignment="TOP_CENTER" />
<font>
<Font size="35" />
</font>
</top>
<bottom>
<HBox spacing="10" alignment="CENTER">
<Button text="Okay" prefWidth="90" />
<Button text="Cancel" prefWidth="90" />
<Button text="Help" prefWidth="90" />
</HBox>
</bottom>
</BorderPane>
这给了
标签
需要 BorderPane.alignment=“CENTER”,而 HBox
需要 alignment=“CENTER”
的原因是因为它们具有不同的可调整大小范围,特别是它们的最大宽度不同。默认情况下,标签的最大宽度是其首选宽度,而
HBox
的最大宽度是无限的。您可以通过在它们上设置背景颜色来查看这一点:
<Label text="this is dialogbox"
BorderPane.alignment="TOP_CENTER"
style="-fx-background-color: aquamarine;"/>
<!-- ... -->
<HBox spacing="10" alignment="CENTER"
style="-fx-background-color: lightskyblue;">
<code>alignment</code>属性将节点的内容定位在其边界内。由于标签在其边界内没有多余的空间用于放置文本,因此使用默认设置时,<code>alignment</code>属性将无效。另一方面,标签的宽度小于边框窗格的顶部区域,因此有空间将其放置在该区域中。BorderPane.lalignment=“CENTER”
属性将整个标签置于边框窗格顶部区域的中心。
相比之下,< code>HBox本身已经填充了边框窗格底部区域的整个宽度。因此在该区域内没有额外的空间来对齐它,因此对于< code>HBox,设置< code > border pane . alignment = " CENTER " 将不起作用。另一方面,< code>HBox本身的空间比按钮所需的空间多,因此可以使用< code>HBox的< code>alignment="CENTER"属性在< code>HBox本身内对齐按钮(< code>HBox的内容)。
如果需要,可以更改最大宽度以实现相同的效果。例如:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.Double?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="this is dialogbox"
alignment="CENTER"
style="-fx-background-color: aquamarine;">
<maxWidth>
<Double fx:constant="MAX_VALUE"/>
</maxWidth>
</Label>
<font>
<Font size="35" />
</font>
</top>
<bottom>
<HBox spacing="10" alignment="CENTER"
style="-fx-background-color: lightskyblue;">
<Button text="Okay" prefWidth="90" />
<Button text="Cancel" prefWidth="90" />
<Button text="Help" prefWidth="90" />
</HBox>
</bottom>
</BorderPane>
允许标签增长(类似于< code>HBox的默认设置),因此现在它的< code>alignment属性具有了预期的效果:
或者
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Region?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="this is dialogbox"
BorderPane.alignment="CENTER"
style="-fx-background-color: aquamarine;" />
<font>
<Font size="35" />
</font>
</top>
<bottom>
<HBox spacing="10" BorderPane.alignment="CENTER"
style="-fx-background-color: lightskyblue;">
<maxWidth>
<Region fx:constant="USE_PREF_SIZE" />
</maxWidth>
<Button text="Okay" prefWidth="90" />
<Button text="Cancel" prefWidth="90" />
<Button text="Help" prefWidth="90" />
</HBox>
</bottom>
</BorderPane>
使HBox
的行为类似于按钮,因此现在它的BorderPlane.alignment
提供了所需的效果: