[英]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.