簡體   English   中英

javafx關閉其他類的窗口

[英]javafx close window from other class

我正在一個小的javaFX窗口中連接到服務器。 連接由新線程建立。 但是我希望在建立連接時關閉窗口。 我如何做到這一點?

這是我嘗試過的。

package se.skplay.scaenicos.subWindows.casparConnection;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import se.skplay.scaenicos.Connections;

public class CasparConnectonController implements Initializable {

    @FXML private TextField hostFiled = new TextField();
    @FXML private TextField portFiled = new TextField();
    @FXML private Button cancel;
    @FXML private Button connect;
    @FXML private Label connecting = new Label();

    private int port = 5250;
    private String host = "localhost";
    static private Stage stage;

    @FXML
    protected void connect() throws IOException {
            if (!hostFiled.getText().equals("")) {
                host = hostFiled.getText();
            }
            if (!portFiled.getText().equals("")) {
                port = Integer.parseInt(portFiled.getText());
            }
            connecting.setText("Connecting...");

            stage = (Stage) connect.getScene().getWindow();
            Connect conCasp = new Connect(host, port);
            conCasp.start();

        try {
            if (Connections.getCaspar().isConnected()) {
                closeWindow();
            }
        } catch (NullPointerException e) {

        }

    }

    private void closeWindow() {
        stage = (Stage) connect.getScene().getWindow();
        stage.close();
    }

    @FXML
    protected void cancel() {
        closeWindow();
    }

    public static Stage getStage() {
        return stage;
    }


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

    }

}

連接到服務器的類

package se.skplay.scaenicos.subWindows.casparConnection;

import java.io.IOException;

import se.skplay.scaenicos.Connections;

public class Connect extends Thread {

    private String host;
    private int port;

    public Connect(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() {
        try {
            connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void connect() throws IOException {
        Connections.setCaspar(host, port);
        CasparConnectonController.getStage().close();
    }

}

使您的Connect類成為Task<Void>的子類:

package se.skplay.scaenicos.subWindows.casparConnection;

import java.io.IOException;

import javafx.concurrent.Task ;

import se.skplay.scaenicos.Connections;

public class Connect extends Task<Void> {

    private String host;
    private int port;

    public Connect(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Void call() {
        try {
            connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void connect() throws IOException {
        Connections.setCaspar(host, port);
        // You can never close a stage from a background thread
        // CasparConnectonController.getStage().close();
    }

}

然后,您可以使用onSucceeded處理程序關閉該階段:

@FXML
protected void connect() throws IOException {
        if (!hostFiled.getText().equals("")) {
            host = hostFiled.getText();
        }
        if (!portFiled.getText().equals("")) {
            port = Integer.parseInt(portFiled.getText());
        }
        connecting.setText("Connecting...");

        stage = (Stage) connect.getScene().getWindow();
        Connect conCasp = new Connect(host, port);

        conCasp.setOnSucceeded(event -> closeWindow());

        new Thread(conCasp).start();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM