Java FX onAction

时间:2017-01-16 14:54:02

标签: javafx

在处理这个项目时,我遇到了如何在第二个窗口中获取按钮以触发onAction的问题。我可以得到第一个窗口打开第二个窗口,但我似乎无法弄清楚如何让第二个窗口中的按钮使用onAction

第二个窗口的代码:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>

<AnchorPane id="AnchorPane" fx:id="createPersonAnchor" prefHeight="247.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"  >
   <children>
      <Button layoutX="171.0" layoutY="180.0" mnemonicParsing="false" onAction="#cancelCreatePersonAction" prefHeight="37.0" prefWidth="79.0" text="Cancel" />
      <TextField layoutX="115.0" layoutY="28.0" />
      <TextField layoutX="115.0" layoutY="78.0" />
      <Button layoutX="47.0" layoutY="180.0" mnemonicParsing="false" onAction="#createPersonAction" prefHeight="37.0" prefWidth="79.0" text="Create" />
      <Text layoutX="29.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="First Name:" wrappingWidth="69.677734375" />
      <Text layoutX="29.0" layoutY="95.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Last Name:" wrappingWidth="69.677734375" />
   </children>
</AnchorPane>

控制器代码:

    package project1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
 * FXML Controller class
 *
 * @author Fomnus
 */
public class FXMLCreatePersonController implements Initializable {

    /**
     * Initializes the controller class.
     */
    public FXMLCreatePersonController(){
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLCreatePerson.fxml"));
        loader.setController(this);
        try{
            loader.load();
        }catch(Exception e){
            System.out.println(e.getMessage()+ "FXMLCreatePersonController failed");
        }
    }
    @FXML AnchorPane createPersonAnchor;
    @FXML public void setFXMLCreatePersonAnchorPane(AnchorPane fxmlCreatePerson){
        createPersonAnchor = fxmlCreatePerson;
    }
    @FXML public AnchorPane getFXMLCreatePersonAnchorPane(){
        return createPersonAnchor;
    }
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

主控制器代码:

package project1;

import java.awt.Desktop.Action;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 *
 * @author fomnus
 */
public class FXMLDashboardController implements Initializable {


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void createPersonWindow(ActionEvent event) {

        FXMLCreatePersonController createpersoncntrl = new FXMLCreatePersonController();

        AnchorPane fxmlCreatePersonAnchorPane = createpersoncntrl.getFXMLCreatePersonAnchorPane();

        StackPane fxmlCreatePersonstackpane = new StackPane();

        fxmlCreatePersonstackpane.getChildren().add(fxmlCreatePersonAnchorPane);

        Scene fxmlCreatePersonscene = new Scene(fxmlCreatePersonstackpane);

        Stage fxmlCreatePersonstage = new Stage();

        fxmlCreatePersonstage.setScene(fxmlCreatePersonscene);

        fxmlCreatePersonstage.show();

    }

}

0 个答案:

没有答案