首页 > 解决方案 > FXML 文件中的 For 循环 || 如何将数据从控制器发送到 fxml 文件?

问题描述

所以我正在开发一个 javaFX 应用程序,我想<ImageView>使用 for 循环创建多个!

是否可以在 .fxml 文件中进行 for/foreach 循环?

如果是,那么如何?

另外我还有一个问题!如何将数据从控制器发送到 sample.fxml 文件?例如,我想将表格表单 controller.java 发送到 sample.fxml 文件并使用该 table+for 循环<ImageView>在 fxml 文件中制作!

注意:我使用 fxml 来显示图像,因为我将这些图像用作按钮。

这是 sample.fxml 的代码:

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">


    <ImageView fitHeight="120" fitWidth="120" fx:id="panda" GridPane.columnIndex="0" GridPane.rowIndex="0" onMousePressed="#mousePressed">
        <image>
            <Image  url="@pics/panda.png">

            </Image>
        </image>
    </ImageView>

</GridPane>

这是 Controller.java 的代码:

package sample;

import javafx.scene.input.MouseEvent;
import javafx.scene.media.AudioClip;

public class Controller {

    public void play_audio()
    {
        AudioClip sound = new AudioClip(this.getClass().getResource("voices/panda.mp3").toString());
        sound.play();
    }

    public void mousePressed() {
        play_audio();
    }
}

Main.java 的代码:

package sample;

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

public class Main extends Application {

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

        primaryStage.setTitle("Animal Sound");


        Scene scene = new Scene(root, 790, 675);
        scene.getStylesheets().add("sample/styles.css");
        primaryStage.setScene(scene);

        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

标签: javajavafxfxml

解决方案


我认为流控制在 fxml 文件中是不可管理的。此外,您并没有真正将参数传递给 fxml 文件。

我认为你已经很好地分离了这一点,再减少一点应该让它发挥作用。我建议从控制器中指定声音和图像。所以我会为每个按钮制作控制器。

package sample;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;

public class AudioButtonController {
    String sound;
    @FXML
    ImageView image;
    public void setAudioLocation(String resourcePath){
        sound = resourcePath;
    }
    public void setImageLocation(String img){
        image.setImage( new Image( img ) );
    }
    public void mousePressed() {
        System.out.println(sound);
    }
}

现在你的每个按钮的fxml都可以了。

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<HBox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.AudioButtonController">
  <ImageView 
      fitHeight="120" fitWidth="120" fx:id="image" onMousePressed="#mousePressed">
  </ImageView>
</HBox>

这是一个示例主类,它启动并加载 2 个音频按钮,但它可以用于加载 N 个按钮。

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application{

@Override
    public void start(Stage primaryStage) throws Exception{
        FlowPane root = new FlowPane(5, 5);

        primaryStage.setTitle("Animal Sound");

        String[] imgs = { "https://conserveblog.files.wordpress.com/2016/05/flagship-panda-thumbnail.jpeg?w=188", "http://news.bbc.co.uk/media/images/38625000/jpg/_38625095_021223panda150.jpg" };
        for(String img: imgs){
            FXMLLoader loader = new FXMLLoader( getClass().getResource("audio_button.fxml") );
            Parent audioButton = loader.load();
            AudioButtonController abc = loader.getController();
            abc.setAudioLocation("not supported");
            abc.setImageLocation(img);
            root.getChildren().add(audioButton);
        }



        Scene scene = new Scene(root, 790, 675);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

}

这个例子有点麻烦。如果您的按钮有更多的布局控件,并且您的外部布局更复杂,那么这可以节省一些时间。


推荐阅读