应用程序启动时运行JFXDialog

时间:2016-11-07 20:26:52

标签: java xml javafx

目前,我正在建立一个客户端服务器聊天GUI来发送和接收消息。我想要的是当应用程序启动时,JFXDialog打开并询问用户名,名字,姓氏和服务器IP地址。 我设法在initialize方法的控制器类中创建一个。我为JFXDialog做了一个fxml,我想用它代替(缩短控制器类中的代码)。有没有办法做到这一点?

JFXDialog来自 JFoenix

所有代码都位于Github Gist

ClientChatController.java是控制器类。

clientGUI.fxml是包含要显示的所有元素的主GUI文件。

dialog.fxml是需要使用的文件,而不是控制器类中的硬编码值。

目前ClientChatController.java中的硬编码代码正在运行。我想用dialog.fxml文件替换它。应用程序启动时应显示该对话框。

据我所知,JFXDialog需要成为StackPane的孩子。

注意:不在StackOverFlow上发布代码的原因是因为在Github Gist上更容易看到它。

控制器类

public class ClientChatController implements Initializable
{
    private ArrayList<Label> users = new ArrayList<Label>();
    @FXML
    private JFXListView<Label> userList;
    @FXML
    private JFXListView<Label> conversationList;
    @FXML
    private StackPane stackPane;
    @FXML
    private JFXTextField username;
    @FXML
    private JFXTextField firstname;
    @FXML
    private JFXTextField lastname;
    @FXML
    private JFXTextField serverIP;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

        //open dialog that contains text field for username, firstname,
        //lastname and server IP address

        JFXDialogLayout content = new JFXDialogLayout();
        content.setHeading(new Text("Setup"));
        VBox vbox = new VBox(10);
        vbox.getChildren()
            .addAll(username, firstname, lastname, serverIP);
        content.setBody(vbox);
        JFXDialog dialog = new JFXDialog(stackPane, content, JFXDialog.DialogTransition.CENTER);
        JFXButton button = new JFXButton("Connect");
        button.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                System.out.println("Username: " + username.getText());
                System.out.println("Firstname: " + firstname.getText());
                System.out.println("Lastname: " + lastname.getText());
                System.out.println("ServerIP: " + serverIP.getText());
                dialog.close();
            }
        });

        content.setActions(button);
        dialog.show();
    }

    @FXML
    public void clientSetup(ActionEvent event)
    {
        //NOT BEING USED AT THE MOMENT
        System.out.println("Username: " + username);
        System.out.println("Firstname: " + firstname);
        System.out.println("Lastname: " + lastname);
        System.out.println("ServerIP: " + serverIP);
    }
}

用于显示内容的fxml

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

<?import com.jfoenix.controls.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
    <StackPane fx:id="stackPane"
                xmlns="http://javafx.com/javafx/8.0.112-ea"
                xmlns:fx="http://javafx.com/fxml/1"
                fx:controller="client.gui.ClientChatController">
        <BorderPane >
           <left>
               <ScrollPane fitToHeight="true"
                           fitToWidth="true">
                   <content>
                       <JFXListView fx:id="userList"/>
                   </content>
               </ScrollPane>
           </left>
           <right>
               <BorderPane>
                   <center>
                       <ScrollPane fitToWidth="true">
                           <content>
                               <JFXListView fx:id="conversationList"/>
                           </content>
                       </ScrollPane>
                   </center>
                   <bottom>
                       <BorderPane>
                           <padding>
                               <Insets bottom="5"
                                       top="1"/>
                           </padding>
                           <center>
                               <JFXTextArea fx:id="textArea"
                                            maxHeight="92"
                                            maxWidth="450"
                                            promptText="Type message"
                                            disable="true">
                               </JFXTextArea>
                           </center>
                           <right>
                               <JFXButton fx:id="sendBtn"
                                          alignment="CENTER"
                                          buttonType="FLAT"
                                          disable="true"
                                             maxHeight="1.7976931348623157E308"
                                          maxWidth="110.0"
                                          ripplerFill="#4059a9"
                                          text="Send"
                                          textAlignment="CENTER">
                               </JFXButton>
                           </right>
                       </BorderPane>
                   </bottom>
               </BorderPane>
           </right>
       </BorderPane>
    </StackPane>
对话框我将使用的文件而不是ClientChatController.java中的硬编码代码

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

<?import com.jfoenix.controls.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<StackPane
        xmlns:fx="http://javafx.com/fxml/1"
        xmlns="http://javafx.com/javafx/2.2"
        fx:controller="client.gui.ClientChatController">
    <JFXDialog fx:id="setupDialog">
        <JFXDialogLayout>
            <heading>
                <Label>Setup</Label>
            </heading>
            <body>
                <VBox spacing="10">
                    <JFXTextField fx:id="username"
                                  promptText="Username"
                                  maxWidth="20">

                    </JFXTextField>
                    <JFXTextField fx:id="firstName"
                                  promptText="First name"
                                  maxWidth="20">

                    </JFXTextField>
                    <JFXTextField fx:id="lastname"
                                  promptText="Last name"
                                  maxWidth="20">

                    </JFXTextField>
                    <JFXTextField fx:id="serverIP"
                                  promptText="Server IP address"
                                  maxWidth="20">

                    </JFXTextField>
                </VBox>
            </body>
            <actions>
                <JFXButton fx:id="connectBtn">Connect</JFXButton>
            </actions>
        </JFXDialogLayout>
    </JFXDialog>
</StackPane>

0 个答案:

没有答案