简体   繁体   English

如何定位我的按钮?

[英]How to position my buttons?

Okay so I have this code in FXML and I want the four bottom buttons to be all directly next to each other and stuff but I can't figure out how to position them at all using CSS. 好的,所以我在FXML中有此代码,我希望底部的四个按钮彼此直接相邻,但我无法弄清楚如何使用CSS放置它们。

Can someone tell me how? 有人可以告诉我如何吗? Here's my code for some reason no matter what I try they don't move at all. 由于某些原因,这是我的代码,无论我尝试什么,它们都不会移动。

Here's my CSS: 这是我的CSS:

.root {
}

.button {
    -fx-background-color:rgb(255.0,255.0,255.0);
    -fx-effect: dropshadow( three-pass-box , rgba(0.0,0.0,0.0,0.6) , 5.0, 0.0 ,0.0 , 1.0 );

}

.button:hover{
    -fx-background-color:rgb(127.0,127.0,127.0);
    -fx-effect: dropshadow( three-pass-box , rgba(0.0,0.0,0.0,0.6) , 5.0, 0.0 ,0.0 , 1.0     );

}

Here's my FXML: 这是我的FXML:

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

<?import javafx.scene.layout.BorderPane?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Label?>

<BorderPane prefHeight="270.0" prefWidth="368.0"
    xmlns:fx="http://javafx.com/fxml" fx:controller="application.FX_TimerController">
    <!-- TODO Add Nodes -->

    <!-- Top Starts Here -->
    <top>
        <SplitPane dividerPositions="0.50">
            <items>
                <AnchorPane>
                    <children>
                        <Button mnemonicParsing="false" id="fx_btnStart" text="Start" />
                    </children>
                </AnchorPane>

                <AnchorPane>
                    <children>
                        <Button mnemonicParsing="false" id="fx_btnStop" text="Stop" />
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
    </top>
        <!-- Top Ends Here -->

    <center>
        <Label id="fx_timer_Label" />
    </center>


    <bottom>
        <GridPane id="Bob">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
                    prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0"
                    vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <Button mnemonicParsing="false" text="Up" />
                <Button mnemonicParsing="false" text="Down"
                    GridPane.columnIndex="1" />
                <Button mnemonicParsing="false" text="Up"
                    GridPane.columnIndex="2" />
                <Button mnemonicParsing="false" text="Down"
                    GridPane.columnIndex="3" />
            </children>
        </GridPane>
    </bottom>

The issue you are facing in GridPane is because of the columnContraints . 您在GridPane中面临的问题是因为columnContraints Remove the columnContraints from your fxml and your buttons are placed next to each other 从fxml中删除columnContraints ,并将按钮彼此相邻放置

<bottom>
    <GridPane id="Bob">
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0"
                vgrow="SOMETIMES" />
        </rowConstraints>
        <children>
            <Button mnemonicParsing="false" text="Up" />
            <Button mnemonicParsing="false" text="Down"
                GridPane.columnIndex="1" />
            <Button mnemonicParsing="false" text="Up"
                GridPane.columnIndex="2" />
            <Button mnemonicParsing="false" text="Down"
                GridPane.columnIndex="3" />
        </children>
    </GridPane>
</bottom>

Approach 2 方法2

If you flexible with using any layout for the , I would recommend replacing the GridPane with a HBox . 如果您灵活地使用任何布局,我建议您将GridPane替换为HBox As GridPanes should have column constraints ! 由于GridPanes应该具有列约束!

<bottom>
    <HBox id="Bob">
        <children>
            <Button mnemonicParsing="false" text="Up" />
            <Button mnemonicParsing="false" text="Down"/>
            <Button mnemonicParsing="false" text="Up"/>
            <Button mnemonicParsing="false" text="Down"/>
        </children>
    </HBox>
</bottom>

One thing is that you can download and use JavaFX Scene Builder for ease. 一件事是,您可以轻松下载和使用JavaFX Scene Builder。 You can graphically position everything where ever you want. 您可以图形化地将所有内容放置在所需的位置。

To define custom position you can do something like this in your button tag. 要定义自定义位置,您可以在button标签中执行类似的操作。

    <Button fx:id="button" mnemonicParsing="false" text="Up" layoutX="80.0" layoutY="100.0"
     onAction="#handleButtonAction" />

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

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