简体   繁体   English

如何向 Java FX 中公共类控制器中的另一个按钮添加功能?

[英]How can I add functionality to another button inside public class Controller in Java FX?

I have a main screen with one buttom that opens another window.我有一个带有一个按钮的主屏幕,可以打开另一个窗口。 However I don't know how to make the buttom close the second window.但是我不知道如何让按钮关闭第二个窗口。 The error that I am getting is because I am trying to handle two button within the same class (handleButtonClick).我得到的错误是因为我试图处理同一个类中的两个按钮(handleButtonClick)。 Someone could help me how to declare and add functionality to this second button?有人可以帮助我如何向第二个按钮声明和添加功能吗? The soncend button calls closePopUp. Soncend 按钮调用 closePopUp。

public class Controller {

    public Button signupB;
    public Label label1;
    public Button closePopUp;


    public void handleButtonClick() {

        System.out.println("Test one");
        label1.setText("This option is not available in the beta version");
        label1.setOpacity(1);

        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("sceneNotAvailable.fxml"));

            Scene scene = new Scene(fxmlLoader.load(), 630, 400);
            Stage stage = new Stage();
            stage.setTitle("New Window");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            Logger logger = Logger.getLogger(getClass().getName());
            logger.log(Level.SEVERE, "Failed to create new Window.", e);
        }

    }
}

Hi guys I found the solution for this situation.大家好,我找到了这种情况的解决方案。 I am very new to JavaFX but what I did was to declare two different classes for each button.我对 JavaFX 很陌生,但我所做的是为每个按钮声明两个不同的类。 I am not sure if this is the right way to go but it works.我不确定这是否是正确的方法,但它有效。

public void handleButtonClick() {
    label1.setText("This option is not available in the beta version");
    label1.setOpacity(1);

    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("sceneNotAvailable.fxml"));

        Scene scene = new Scene(fxmlLoader.load(), 630, 400);
        Stage stage = new Stage();
        stage.setTitle("New Window");
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {
        Logger logger = Logger.getLogger(getClass().getName());
        logger.log(Level.SEVERE, "Failed to create new Window.", e);
    }
}

public void Button1Action(ActionEvent actionEvent) {
    Stage stage = (Stage) closePopUp.getScene().getWindow();
    stage.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中重复公共课程? - How can I repeat a public class in Java? 如何使用 Java FX 更正此错误? - How can I correct this errror with Java FX? 如何多次从另一个 class 调用 JAVA FX Class - How to call a JAVA FX Class from another class many times 如何在 Java 的 canvas 中添加此按钮? - How can i add this button inside the canvas of Java? 如何从 Java 应用程序中的另一个类访问在实用程序类中声明的公共最终静态列表? - How can I access to a public final static list declared into an utility class from another class in a Java application? 如何访问位于Java中另一个类的另一个方法内的类内的方法 - How can I access a method that is inside a class that is inside another method of another class in Java 如何在 java fx 的另一个按钮方法中访问图像变量 - how to access a image variable in another button method in java fx 如何在Swing中的两个按钮中添加“默认按钮”功能? - How can I add “Default Button” functionality to two buttons in Swing? 为了清楚起见,如何将MainActivity功能拆分为另一个类? - How can I split MainActivity functionality into an another class for clarity? 如何在Java小程序中使用JMF功能? - How can I use JMF functionality inside a java applet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM