将窗格添加到HBox的更有效方法?

问题描述 投票:0回答:1

我正在开发一个项目,该项目将涉及向非常宽的HBox添加窗格(如果效果更好,则为hbox)。我很容易在fxml中设置它,但我已经不得不做一些这些,我很想避免所有的复制粘贴。所以我想我应该尝试使用for循环来填充HBox,但无论出于什么原因它都无法正常工作。我正处于最初阶段,因此我的代码非常简单直接。

这是我试图重新创建的基本示例

<HBox>
   <children>
      <Pane fx:id="pane0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
         </children></Pane>
      <Pane fx:id="pane1" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
         </children></Pane>
      <Pane fx:id="pane2" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
         </children></Pane>
   </children>
</HBox>

因此,为了动态创建它(为了清晰起见),我为HBox和窗格创建了一个类。现在每个窗格只有一个按钮,但是一旦这个代码工作,它们将进行进一步的自定义。与HBox相同。

这是HBox

public class HBoxTestClass {

    @FXML
    HBox hBox = new HBox();

    public HBoxTestClass(){

    }
    @FXML
    public void initialize(){
        populateHBox();
    }
    private void populateHBox(){
        for (int i = 0; i < 3; i++){
            hBox.getChildren().add(new TestPane());
            hBox.setSpacing(10);
        }
    }
}

它的fxml

<HBox fx:id="hBox" xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="sample.HBoxTestClass"
            prefHeight="400.0" prefWidth="600.0">

</HBox>

窗格类及其fxml

public class TestPane extends Pane{

    @FXML Pane testPane = new Pane();
    @FXML Button button = new Button();

    public TestPane(){

    }
    @FXML
    private void initialize(){
        button.setText("Click Me!");
    }
}
<Pane fx:id="testPane" xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="sample.TestPane"
            prefHeight="200.0" prefWidth="200.0">
    <children>
        <Button fx:id="button" layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
    </children>

</Pane>

所以我上面非常简单的代码只生成一个空白的宽屏幕,里面没有面板。我在我的TestPane类的构造函数中放置了一个控制台打印,所以我知道它已被调用,但仍然没有出现。有什么建议?谢谢

javafx scenebuilder pane hbox
1个回答
0
投票

我不确定你在代码中做了什么。像@FXML Pane testPane = new Pane();这样的代码看起来应该像@FXML Pane testPane;。我不确定如何在你的程序中调用像@FXML public void initialize(){这样的代码。尝试遵循此MCVE。

主要

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication363 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

调节器

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private HBox hBox;

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
        for (int i = 0; i < 5; i++) {
            StackPane stackPane = new StackPane(new Label("Label: " + i));
            hBox.getChildren().add(stackPane);
        }
    }

}

FXML

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

<?import javafx.scene.layout.HBox?>


<HBox fx:id="hBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication363.FXMLDocumentController" />
© www.soinside.com 2019 - 2024. All rights reserved.