[英]How to create a close button with JavaFX and FXML
I'm new to the community and I don't really know how to ask for help here.我是社区的新手,我真的不知道如何在这里寻求帮助。 I was trying to create a close button for my alert pop Up window, but I'm getting a syntax error.
我试图为我的警报弹出窗口创建一个关闭按钮,但出现语法错误。 i also would like to perform some actions before the window closes completely.
我还想在窗口完全关闭之前执行一些操作。
This is the driver that calls the window to appear:这是调用窗口出现的驱动程序:
package sample;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.awt.*;
import java.io.IOException;
public class Controller {
public static String name;
public void removeButtonPressed() throws IOException {
name = "Hello world";
alertController a = new alertController().starWindow();
}
}
This is the alert's controller这是警报的控制器
package sample;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class alertController implements Initializable {
@FXML
private Label label = new Label();
private String s = Controller.name;
public alertController starWindow() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("alert.fxml"));
Stage window = new Stage();
window.initModality((Modality.APPLICATION_MODAL));
window.setTitle("");
Scene scene = new Scene(root);
window.setScene(scene);
window.show();
return null;
}
private void closeButton(MouseEvent event){
Stage s = (Stage) ((Node)event.getSource().getScene().getWindow());
/*I'm getting problems with this part of my code
the IDE gives me trouble with getScene()*/
s.close();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
label.setText(s);
}
}
this is the alert's FXML file这是警报的 FXML 文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="141.0" maxWidth="394.0" prefHeight="119.0" prefWidth="307.0" style="-fx-background-radius: 5;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.alertController">
<children>
<AnchorPane>
<children>
<Label fx:id="label" layoutX="28.0" layoutY="20.0" maxHeight="35.0" maxWidth="149.0" text="Are You Sure?">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<Separator maxWidth="307.0">
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</Separator>
<HBox prefHeight="38.0" prefWidth="297.0" spacing="10.0">
<children>
<Region HBox.hgrow="ALWAYS" />
<Button maxHeight="44.0" maxWidth="58.0" mnemonicParsing="false" text="Yes">
<font>
<Font size="15.0" />
</font>
</Button>
<Button maxHeight="44.0" maxWidth="55.0" mnemonicParsing="false" onMouseClicked="#closeButton" text="No">
<HBox.margin>
<Insets right="30.0" />
</HBox.margin>
<font>
<Font size="15.0" />
</font>
</Button>
</children>
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</HBox>
</children>
</VBox>
I also tried to use setOnAction but the same error happened.我也尝试使用 setOnAction 但发生了同样的错误。
thank you so much!!太感谢了!!
Try using onAction="#closeButton"
in your FXML code.尝试在您的 FXML 代码中使用
onAction="#closeButton"
。 And I think that this method should be public as well as every node on your stage (sa the label that you've annotated with @FXML
).而且我认为这个方法应该是公开的,以及你舞台上的每个节点(你用
@FXML
注释的标签)。
Furthermore you can access the stage by any object on that stage with此外,您可以通过该舞台上的任何对象访问舞台
Stage stage = (Stage) label.getScene().getWindow();
Hope this helps!希望这可以帮助!
Let's use brackets to clarify how the compiler interprets让我们用括号来阐明编译器是如何解释的
Stage s = (Stage) ((Node)event.getSource().getScene().getWindow());
Stage s = (Stage) ((Node) (((event.getSource()).getScene()).getWindow()));
ie the cast to Node
has lower precedence than the .
即转换到
Node
优先级低于.
operator leaving you with an expression of type Object
you try to call .getSource()
on.运算符给你留下一个你尝试调用
.getSource()
Object
类型的表达式。 Object
does not contain a getScene
method. Object
不包含getScene
方法。 Make sure the cast is applied first by adding brackets:通过添加括号确保首先应用演员表:
Stage s = (Stage) ((Node) event.getSource()).getScene().getWindow();
Also note that even after applying this fix you'll still have issues that will result in exceptions at runtime: You're using some java.awt
imports.另请注意,即使应用此修复程序后,您仍然会遇到会导致运行时异常的问题:您正在使用一些
java.awt
导入。 You need to replace those with the appropriate javafx imports.您需要用适当的 javafx 导入替换它们。
I also strongly recommend adhering to the java naming conventions.我还强烈建议遵守 java 命名约定。 Starting type names with a lowercase letter can make your code hard to read.
以小写字母开头的类型名称会使您的代码难以阅读。
This was the solution to my problem.这是我的问题的解决方案。
Controller:控制器:
@FXML
private void closeButton(Event event){
Stage s = (Stage) ((Node) event.getSource()).getScene().getWindow();
s.close();
}
FXML code: FXML 代码:
<Button maxHeight="44.0" maxWidth="55.0" mnemonicParsing="false" onAction="#closeButton" text="No">
I deleted:我删除了:
import java.awt.event.MouseEvent;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.