如何在javaFX中不点击按钮从其他类中打开弹出窗口?

问题描述 投票:0回答:1

所以我在javaFX控制器类中有一个叫做 "popup "的方法,它可以在实际的应用程序窗口上面打开一个小的弹出窗口。如果在fxml中把它分配给一个按钮,并且按钮被点击,那么这个方法的运行就没有问题,但这不是我想使用它的方式。

我有另一个叫做'Timer'的类,它有一个新的任务(新的线程),从某一个数字开始倒计时,在某一个点上,它会打开一个弹出窗口,并显示一条消息。我的目的是在这个'Timer'类中调用并运行'popup'方法。当我从这里调用'popup'方法时,它开始执行,但弹出窗口根本没有出现。(方法调用发生在我从'popup'方法在控制台得到 "in popup "信息的时候。) )

那么,为什么点击按钮从fxml文件中调用'popup'方法时能正常工作,而从其他类中调用时为什么不能?谢谢。

请看下面带有'popup'方法的控制器类和Timer类(在项目中使用Gradle)。

"SceneController "控制器类。

package GradleFX;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;


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

//import java.awt.event.ActionEvent;

public class SceneController implements Initializable {

    public static String password = "";
    protected static int timercount = 20;


    @FXML
    private Label PWLabel;

    @FXML
    private Label bottomLabel;

    @FXML
    private PasswordField PWField;
    @FXML
    private Label showPWLabel;

    protected static Label myBottomLabel;
    private static PasswordField myPWField;
    private static Label myShowPWLabel;

    private static int tries;

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

        Timer timerTask = new Timer();

        myBottomLabel = bottomLabel;
        myPWField = PWField;
        myShowPWLabel = showPWLabel;

        new Thread(timerTask).start();
    }

    **/***********************************************************************
     /*This method runs if button is pressed in main application,
     but can't make it work by calling it from Timer Class */

    public void popup() {
        System.out.println("in popup");
        Stage dialogStage = new Stage();
        dialogStage.initModality(Modality.WINDOW_MODAL);

        VBox vbox = new VBox(new Text("Hi"), new Button("Ok."));
        vbox.setAlignment(Pos.CENTER);
        vbox.setPadding(new Insets(15));

        dialogStage.setScene(new Scene(vbox));
        dialogStage.show();
    }

    //****************************************************************************
    public void showPW() {
        myShowPWLabel.setText(myPWField.getText());
    }

    public void hidePW() {
        myShowPWLabel.setText("");
    }

    public void exit() {
        System.exit(0);
    }

    public void write() {
        PWLabel.setText("Mukodik");

    }

    public void writeInput(String in) {

        password = in;
        System.out.println("final password text text: " + password);
        writeFinally();

    }

    public void writeFinally() {

        System.out.println("This is 'password' : " + password);
        //bottomLabel.setText(password);
    }

    public void bottomLabelWrite() {
        bottomLabel.setText(myPWField.getText());
    }

    public static void setLabel() throws InterruptedException {
        myBottomLabel.setText("");
        myBottomLabel.setText("Database has been permanently erased.");
        //Thread.sleep(3000);
        //System.exit(0);
    }

    public static void noKeyEnteredNote() {
        myBottomLabel.setTextFill(Color.BLACK);
        myBottomLabel.setText("No key entered. Type Main Key.");
    }

    public static void rightKey() {
        myBottomLabel.setText("Yes, this is the right key.");
    }

    public static void wrongKey() throws InterruptedException {
        tries = MasterKey.numOfTryLeft;
        if (tries > 0) {
            myBottomLabel.setTextFill(Color.RED);
            myBottomLabel.setText("!!!Wrong key!!! You've got " + tries + " tries left!");

        }
    }

    public void simpleTest(String in) {
        System.out.println("in simpleTest and in is: " + in);
    }


    public void getMainKey() throws IOException, InterruptedException {
        MasterKey masterKey = new MasterKey();
        System.out.println("Inside SceneController");

        masterKey.requestKey(myPWField.getText());


    }

    public void changeScreen(ActionEvent event) throws IOException, InterruptedException {

        getMainKey();
        if (MasterKey.isRightKey) {
            Parent tableViewParent = FXMLLoader.load(getClass().getResource("Menu.fxml"));
            Scene tableViewScene = new Scene(tableViewParent);

            Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
            window.setScene(tableViewScene);
            window.show();
        }
    }


}

这是Timer类

    package GradleFX;


import javafx.concurrent.Task;
import javafx.event.ActionEvent;


public class Timer extends Task {

    private ActionEvent actionEvent;

    @Override
    protected Integer call() throws Exception {
        boolean notCalled = true;

        while (SceneController.timercount > 0) {
            SceneController sceneController = new SceneController();


            System.out.println(SceneController.timercount);
            Thread.sleep(1000);
            SceneController.timercount--;
            if (SceneController.timercount < 19) {

                System.out.println("Less than 5");

                if(notCalled) {
                    sceneController.popup();
                    notCalled = false;
                }



            }


        }
        System.exit(0);

        return null;
    }


}
java user-interface javafx scenebuilder popupwindow
1个回答
0
投票

把这个添加到你的代码中。

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

    Timer timerTask = new Timer();

    myBottomLabel = bottomLabel;
    myPWField = PWField;
    myShowPWLabel = showPWLabel;

    new Thread(timerTask).start();
    timerTask.setOnFinished(e->{
        popup();
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.