JavaFX 2.0 FXML儿童Windows [英] JavaFX 2.0 FXML Child Windows

查看:78
本文介绍了JavaFX 2.0 FXML儿童Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多次搜索,我发现了这个问题如何创建一个javafx 2.0应用程序MDI 。我真正想知道的是,如果我可以使用JavaFX组件和Scene Builder创建弹出窗口或子窗口到主窗口来创建新窗口。

After much searching I found this question How to create a javafx 2.0 application MDI. What I really wanted to know is if I can create a pop-up window or child window to the main window using JavaFX components and Scene Builder to create the new window.

我最终得到了一个模态弹出窗口:

I ended up with this for a modal pop-up window:

在Main类中我想将主要阶段保存到我可以从主控制器类访问的字段。所以,我在 Main.Start()方法中为它添加了一个静态变量 Stage

In the Main class I wanted to save the primary stage to a field I can access from my primary controller class. So, I added a static variable Stage to it and this in the Main.Start() method:

primaryController.primaryStage = primaryStage;

这是primaryController中的按钮使用的方法:

This the method that a button in the primaryController uses:

public void OnBtnShowChild(ActionEvent event) {
    MessageBoxController msgBox = new MessageBoxController();
    try {
        msgBox.showMessageBox(primaryStage);
    } catch (Exception e) {
        e.printStackTrace(); 
    }
}

这是我在帮助下创建的MessageBoxController类场景生成器。它具有标准弹出框的基本布局,可用于显示Icon(ImageView),TextBox(用于消息文本)和两个按钮(用于YES / NO功能)。我不确定如何将按钮的结果传递给primaryController。

This is the MessageBoxController class that I created with help from Scene Builder. It has the basic layout of a standard pop-up box that can be used to display an Icon (ImageView), TextBox (for your message text), and two buttons (for YES/NO functionality). I am not sure yet how to have it communicate the results of what button was pressed back to the primaryController.

public class MessageBoxController implements Initializable {

@FXML
// fx:id="btnNo"
private Button btnNo; // Value injected by FXMLLoader

@FXML
// fx:id="btnYes"
private Button btnYes; // Value injected by FXMLLoader

@FXML
// fx:id="imgMessage"
private ImageView imgMessage; // Value injected by FXMLLoader

@FXML
// fx:id="txtMessage"
private TextField txtMessage; // Value injected by FXMLLoader

private Stage myParent;
private Stage messageBoxStage;

public void showMessageBox(Stage parentStage) {
    this.myParent = parentStage;

    try {
        messageBoxStage = new Stage();
        AnchorPane page = (AnchorPane) FXMLLoader.load(MessageBoxController.class.getResource("/MessageBox/MessageBoxFXML.fxml"));
        Scene scene = new Scene(page);
        messageBoxStage.setScene(scene);
        messageBoxStage.setTitle("Message Box");
        messageBoxStage.initOwner(this.myParent);
        messageBoxStage.initModality(Modality.WINDOW_MODAL);
        messageBoxStage.show();
    } catch (Exception ex) {
        System.out.println("Exception foundeth in showMessageBox");
        ex.printStackTrace();
    }
}
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
    txtMessage.setText("Howdy");

}

public void OnBtnYes(ActionEvent event) {

}

public void OnBtnNo(ActionEvent event) {

}

}

最后,这是我在Scene Builder中创建的FXML文件:

And finally, this is the FXML file I created in Scene Builder:

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

<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"   
      prefHeight="172.0" prefWidth="524.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MessageBox.MessageBoxController">
  <children>
    <VBox prefHeight="172.0" prefWidth="524.0" styleClass="vboxes" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <HBox alignment="CENTER" prefHeight="109.99990000000253" prefWidth="516.0" spacing="30.0">
          <children>
            <ImageView fx:id="imgMessage" fitHeight="110.0" fitWidth="146.66666666666666" pickOnBounds="true" preserveRatio="true" styleClass="null" />
            <TextField fx:id="txtMessage" editable="false" prefHeight="47.0" prefWidth="325.0" />
          </children>
          <stylesheets>
        <URL value="@MyCSS.css" />
      </stylesheets>
    </HBox>
    <HBox alignment="CENTER" prefHeight="58.0" prefWidth="516.0" spacing="30.0">
      <children>
        <Button fx:id="btnYes" mnemonicParsing="false" onAction="#OnBtnYes" text="Button" />
        <Button fx:id="btnNo" mnemonicParsing="false" onAction="#OnBtnNo" text="Button" />
      </children>
    </HBox>
  </children>
  <stylesheets>
    <URL value="@MyCSS.css" />
  </stylesheets>
</VBox>
</children>
<stylesheets>
<URL value="@MyCSS.css" />
</stylesheets>
</AnchorPane>

有了这个,我可以创建一个模态弹出窗口,我还想创建其他子窗口用于使用不同控件以其他方式显示数据。而且,最重要的是,我可以使用Scene Builder来创建布局。

With this I can create a modal pop-up window, and I also want to create other child windows for displaying data in other ways using different controls. And, most importantly, I can use Scene Builder to create the layout.

您如何看待?这是一个很好的方法,直到他们在Java 8和JavaFX 8中添加真正的支持?

What do you think? Is this a good way to do this until they add real support in Java 8 and JavaFX 8?

推荐答案

你试过吗?小组课程?你可以添加不同的元素与fxml和控制器。

did you try wit the Group class? you can add diferent elements with fxml and controllers.

Group root= new Group(); 
AnchorPane frame=FXMLLoader.load(getClass().getResource("frame.fxml"));
AnchorPane  content=  FXMLLoader.load(getClass().getResource("principal.fxml"));
root.getChildren().add(window);
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

这篇关于JavaFX 2.0 FXML儿童Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆