Java 对于FXML文件中的循环| |如何将数据从控制器发送到FXML文件?
因此,我正在开发一个javaFX应用程序,我想使用for循环创建多个Java 对于FXML文件中的循环| |如何将数据从控制器发送到FXML文件?,java,javafx,fxml,Java,Javafx,Fxml,因此,我正在开发一个javaFX应用程序,我想使用for循环创建多个 是否可以在.fxml文件中创建for/foreach循环 如果是的话,怎么办 我还有一个问题!如何将数据从控制器发送到sample.fxml文件? 例如,我想向sample.fxml文件发送一个table form controller.java,并使用该table+for循环在fxml文件中生成 注意:我使用fxml显示图像,因为我将这些图像用作按钮。 下面是sample.fxml的代码: <?import javaf
是否可以在.fxml文件中创建for/foreach循环
如果是的话,怎么办
我还有一个问题!如何将数据从控制器发送到sample.fxml文件?
例如,我想向sample.fxml文件发送一个table form controller.java,并使用该table+for循环在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>
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);
}
}
我认为流控制在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>
对于这个例子来说,这有点麻烦。如果您的按钮有更多的布局控件,并且您的外部布局更复杂,那么这可以节省一些时间 我不清楚为什么要在fxml文件中执行此操作?为什么不从控制器中填充未知元素呢?我使用fxml文件将这些图像用作按钮。他们谈论传递参数。我还没有找到任何关于流控制的内容,所以我认为您应该从控制器开始工作。无关:java命名约定!除了下划线之外,你几乎都在遵循它们——使用驼峰格代替标题,我无法为每个按钮手动创建fxml,我不知道我有多少个按钮。这就是为什么我想在fxml中使用for循环(在Controlleru中,我将从一个文件中读取按钮的数量,例如20按钮,我想在fxml文件中使用来生成它们!@Dhia并非所有事情都可以在fxml中完成。视图中更动态的部分应该在代码中实现。当然,您可以通过在控件中实现动态部分,将其与fxml文件混合使用。)oller.我想让columIndex和rowindex在FXML文件中动态,这样按钮就不会相互重叠!我想在FXML中创建Gridpane.rowindex=I!我是在控件中创建的一个变量!如何?还想说声抱歉,因为我在javafx/FXML中是新手:/@DhiaDjobbi我已经更新了答案,现在是一个完整的自圆其说获得的示例。如果运行该命令,您将获得一个包含两个不同图像的窗口。我使用了1个fxml文件加载了它,每次都将其添加到布局中。@DhiaDjobbi您的注释更改了很多,当您通过控制器将组件添加到场景中时,您不需要在fxml中指定行索引知道“我”你告诉gridpane。只在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>
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();
}
}