如何在Javafx和JFXML中绑定Vbox的度假胜地儿童

发布于 2025-02-10 14:55:46 字数 2248 浏览 3 评论 0原文

我有包括网格窗格和Vbox在内的FXML代码,其中包含具有IDS的按钮,

它可以绑定Vbox,以便根据FX:ID求解按钮的顺序?

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="472.0" prefWidth="698.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/18">
  <columnConstraints>
    <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>
      <VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1">
         <children>
            <Button fx:id="1" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="1" />
            <Button fx:id="2" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="2" />
            <Button fx:id="3" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="3" />
            <Button fx:id="4" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="4" />
            <Button fx:id="5" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="5" />
            <Button fx:id="6" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="6" />
            <Button fx:id="7" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="7" />
         </children>
      </VBox>
   </children>
</GridPane>

控制器 控制器具有

        @FXML
            public void initialize() {
List<Integer> ids = new ArrayList<>();        // resort the childs of vbox based on the list "ids"
    
    }

我想将按钮列表绑定到我的“ IDS”变量,该变量包括新的FX-ID,如果列表 - 元素的顺序更改,则还应更改Vbox内部按钮列表的顺序

I have the folloing fxml code which includes grid pane and vbox which includes buttons with its ids

is it possible to bind vbox so that the order of the buttons are resorted based on the fx:id?

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="472.0" prefWidth="698.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/18">
  <columnConstraints>
    <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>
      <VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1">
         <children>
            <Button fx:id="1" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="1" />
            <Button fx:id="2" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="2" />
            <Button fx:id="3" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="3" />
            <Button fx:id="4" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="4" />
            <Button fx:id="5" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="5" />
            <Button fx:id="6" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="6" />
            <Button fx:id="7" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="7" />
         </children>
      </VBox>
   </children>
</GridPane>

The Controller
the controller has

        @FXML
            public void initialize() {
List<Integer> ids = new ArrayList<>();        // resort the childs of vbox based on the list "ids"
    
    }

i want to bind the list of buttons to my "ids" variable which includes the new fx-id`s and if the order of list-elemens changes then the order of list of buttons inside vbox should also be changed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2025-02-17 14:55:46

0
更好地使用来自SortedMap接口的treemap。

SortedMap<Integer, CheckBox> sm = TreeMap<Integer, CheckBox>();

sm.put(new Integer(1),new CheckBox("first checkbox"));
sm.put(new Integer(2),new CheckBox("second checkbox"));
sm.put(new Integer(3),new CheckBox("third checkbox"));
sm.put(new Integer(4),new CheckBox("fourth checkbox"));
sm.put(new Integer(5),new CheckBox("fifth checkbox"));
sm.put(new Integer(6),new CheckBox("sixth checkbox"));
sm.put(new Integer(7),new CheckBox("seventh checkbox"));
....

按照ID的顺序进行遍历后获得它们

0
Better use TreeMap from SortedMap interface.

SortedMap<Integer, CheckBox> sm = TreeMap<Integer, CheckBox>();

sm.put(new Integer(1),new CheckBox("first checkbox"));
sm.put(new Integer(2),new CheckBox("second checkbox"));
sm.put(new Integer(3),new CheckBox("third checkbox"));
sm.put(new Integer(4),new CheckBox("fourth checkbox"));
sm.put(new Integer(5),new CheckBox("fifth checkbox"));
sm.put(new Integer(6),new CheckBox("sixth checkbox"));
sm.put(new Integer(7),new CheckBox("seventh checkbox"));
....

getting them after traversing in the order of the id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文