FXML 文件中的 For 循环 ||如何将数据从控制器发送到 fxml 文件?
For loop in FXML file || How to send data from controller to fxml file?
所以我正在开发一个 javaFX 应用程序,我想使用 for 循环创建多个 <ImageView>
!
是否可以在 .fxml 文件中制作 for/foreach 循环?
如果是,那怎么办?
我还有一个问题!如何将数据从控制器发送到 sample.fxml 文件?
例如,我想将 table 表单 controller.java 发送到 sample.fxml 文件,并使用 table+for 循环在 fxml 文件中创建 <ImageView>
!
注意:我使用 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);
}
}
我认为在 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>
这是一个示例 main class,它启动并加载 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();
}
}
这个例子有点麻烦。如果您的按钮有更多的布局控件,并且您的外部布局更复杂,那么这可以节省一些时间。
所以我正在开发一个 javaFX 应用程序,我想使用 for 循环创建多个 <ImageView>
!
是否可以在 .fxml 文件中制作 for/foreach 循环?
如果是,那怎么办?
我还有一个问题!如何将数据从控制器发送到 sample.fxml 文件?
例如,我想将 table 表单 controller.java 发送到 sample.fxml 文件,并使用 table+for 循环在 fxml 文件中创建 <ImageView>
!
注意:我使用 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);
}
}
我认为在 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>
这是一个示例 main class,它启动并加载 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();
}
}
这个例子有点麻烦。如果您的按钮有更多的布局控件,并且您的外部布局更复杂,那么这可以节省一些时间。