返回javafx中的上一个屏幕,发送值为

时间:2017-02-03 15:46:02

标签: javafx

我在intellij IDE中使用javafx。我认为它可能是Repetitious但我没有看到解决方案。 当我点击屏幕上设置更改按钮时,我想关闭屏幕并返回上一个屏幕,但它打开了新屏幕 当你看到保存值到第一个textfield screen.I想要与fxml GUI而不是javafx代码的solotion。感谢

Main.java:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FMLLoader.load(getClass().getResource("sample1.fxml"));
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

Sample1Controller.java

public class Sample1Controller {
@FXML
private Button btn1;
@FXML
private TextField text1;

void initData(String s) {
    text1.setText(s);
}

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample2.fxml"));  
    Stage stage = new Stage();
    stage.setScene(new Scene((Parent) loader.load()));
    stage.show();
}
}

Sample2Controller.java

public class Sample2Controller {
@FXML
public Button btn2;
@FXML
public TextField text2;
@FXML
public Label lbl;
String string;
@FXML

private void handleButtonAction(ActionEvent event) throws IOException {
    string = text2.getText();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample1.fxml"));
    Stage stage = new Stage();
    stage.setScene(new Scene((Parent) loader.load()));
    Sample1Controller controller =
            loader.getController();
    controller.initData(string);
    stage.show();
}
}

sample1.fxml     

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-   Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"  xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Sample1Controller">
    <children>
    <Button fx:id="btn1" layoutX="246.0" layoutY="135.0" mnemonicParsing="false" onAction="#handleButtonAction" text="go to scene 2 " />
    <Label layoutX="259.0" layoutY="229.0" text="this is scene 1" />
    <TextField fx:id="text1" layoutX="216.0" layoutY="285.0" />
</children>
</AnchorPane>

sample2.fxml     

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: tan;" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Sample2Controller">
<children>
  <Button fx:id="btn2" layoutX="269.0" layoutY="200.0" mnemonicParsing="false" onAction="#handleButtonAction" text="SetChange" />
  <Label layoutX="269.0" layoutY="137.0" text="This is scene 2" />
  <TextField fx:id="text2" layoutX="226.0" layoutY="261.0" />
</children>
</AnchorPane>

2 个答案:

答案 0 :(得分:2)

通常的方法

Sample1Controller你可以做

public class Sample1Controller {

    @FXML
    private Button btn1;
    @FXML
    private TextField text1;


    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample2.fxml"));  
        Stage stage = new Stage();
        stage.initOwner(btn1.getScene().getWindow());
        stage.setScene(new Scene((Parent) loader.load()));

        // showAndWait will block execution until the window closes...
        stage.showAndWait();

        Sample2Controller controller = loader.getController();
        text1.setText(controller.getText());
    }

}

并在Sample2Controller中执行:

public class Sample2Controller {

    @FXML
    private Button btn2;
    @FXML
    private TextField text2;
    @FXML
    private Label lbl;

    private String text;

    public String getText() {
        return text ;
    }

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        text = text2.getText();

        // close this window...
        btn2.getScene().getWindow().hide();
    }

}

不阻止

当您显示第二个窗口时,阻止第一个控制器(带showAndWait())的执行通常是您想要做的;但是,如果您因某些原因不想这样做,可以在窗口关闭时使用监听器进行更新:

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample2.fxml"));  
        Stage stage = new Stage();
        stage.initOwner(btn1.getScene().getWindow());
        stage.setScene(new Scene((Parent) loader.load()));

        Sample2Controller controller = loader.getController();
        stage.setOnHidden(evt -> text1.setText(controller.getText()));

        stage.show();

    }

不关闭窗口

最后,如果您想在窗口未关闭时更新文本(但是,按下按钮),您可以执行以下操作:

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample2.fxml"));  
        Stage stage = new Stage();
        stage.initOwner(btn1.getScene().getWindow());
        stage.setScene(new Scene((Parent) loader.load()));

        Sample2Controller controller = loader.getController();
        controller.setTextCallback(text1::setText);

        stage.show();

    }

然后将SampleController2修改为:

public class Sample2Controller {

    @FXML
    private Button btn2;
    @FXML
    private TextField text2;
    @FXML
    private Label lbl;

    private Consumer<String> textCallback ;

    public void setTextCallback(Consumer<String> textCallback) {
        this.textCallback = textCallback ;
    }

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        if (textCallback != null) {
            textCallback.accept(text2);
        }
    }

}

答案 1 :(得分:1)

尝试以下方法:

@FXML
private AnchorPane container;

private void handleButtonAction(ActionEvent event) throws IOException {
Parent root;
    Stage stage;
    if (event.getSource() == btn2) {
        stage = (Stage) container.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("sample1.fxml"));
        Scene scene = new Scene(root, 1650, 1000);
        stage.setScene(scene);
        stage.show();
    }
}