如何使用JavaFX和FXML创建关闭按钮

如何使用JavaFX和FXML创建关闭按钮,java,javafx,fxml,scenebuilder,Java,Javafx,Fxml,Scenebuilder,我是这个社区的新手,我真的不知道如何在这里寻求帮助。我试图为警报弹出窗口创建一个关闭按钮,但出现语法错误。我还想在窗口完全关闭之前执行一些操作 这是调用窗口以显示的驱动程序: package sample; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Modali

我是这个社区的新手,我真的不知道如何在这里寻求帮助。我试图为警报弹出窗口创建一个关闭按钮,但出现语法错误。我还想在窗口完全关闭之前执行一些操作

这是调用窗口以显示的驱动程序:

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();

    }

}
这是警报的控制器

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);
    }
}
这是警报的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>

我也尝试使用setOnAction,但同样的错误发生了

非常感谢你

尝试在FXML代码中使用
onAction=“#closeButton”
。我认为这个方法应该是公开的,就像你舞台上的每个节点一样(s.a.你用
@FXML
注释的标签)

此外,您还可以通过该平台上的任何对象访问该平台

Stage stage = (Stage) label.getScene().getWindow();

希望这有帮助

让我们用括号来说明编译器是如何解释的

Stage s = (Stage) ((Node)event.getSource().getScene().getWindow());
i、 e.强制转换到
节点
的优先级低于
运算符,留给您的表达式类型为
对象
,您尝试调用
.getSource()
on
Object
不包含
getScene
方法。通过添加括号,确保先应用强制类型转换:

Stage s = (Stage) ((Node) event.getSource()).getScene().getWindow();

还要注意的是,即使应用了此修复程序,您仍然会遇到一些问题,这些问题将导致运行时出现异常:您正在使用一些
java.awt
imports。您需要使用适当的javafx导入来替换这些内容


我还强烈建议遵守java命名约定。以小写字母开头的类型名称会使代码难以阅读。

这就是我问题的解决方案

控制器:

  @FXML
    private void closeButton(Event event){
        Stage s = (Stage) ((Node) event.getSource()).getScene().getWindow();
        s.close();
    }
FXML代码:

<Button maxHeight="44.0" maxWidth="55.0" mnemonicParsing="false" onAction="#closeButton" text="No">

awt
mouseevent导入。我不知道这是否是问题所在,但您不应该使用它。在查看了您的其余代码后,很可能是问题所在。要添加到@Sedrick,因为您的新awt也用于带有gui的swing应用程序,而且awt对Javafx不起作用,请删除
import java.awt.*
并将其与导入javafx.scene.input.MouseEvent交换您的代码有几处错误(f.i.您不能实例化已注入的字段,您不应该在任何地方具有静态访问权限,在获取阶段时缺少右括号,在closebutton处缺少fxml注释…)-在您看来,我会后退几步,重读你最喜欢的课本,开始一个接一个地解决问题:)这能回答你的问题吗?这个方法应该是公开的,你的舞台上的每个节点都应该是公开的,这是可能的,但不是最好的选择(内部不应该公开):缺少的是私有方法/fields上的@fxml注释Hank you Raphael,你的回答真的很有帮助!令人惊叹的!这解决了我的问题!非常感谢你,费边!
<Button maxHeight="44.0" maxWidth="55.0" mnemonicParsing="false" onAction="#closeButton" text="No">
import java.awt.event.MouseEvent;