簡體   English   中英

如何使用 JavaFX 和 FXML 創建關閉按鈕

[英]How to create a close button with JavaFX and FXML

我是社區的新手,我真的不知道如何在這里尋求幫助。 我試圖為我的警報彈出窗口創建一個關閉按鈕,但出現語法錯誤。 我還想在窗口完全關閉之前執行一些操作。

這是調用窗口出現的驅動程序:

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" 而且我認為這個方法應該是公開的,以及你舞台上的每個節點(你用@FXML注釋的標簽)。

此外,您可以通過該舞台上的任何對象訪問舞台

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

希望這可以幫助!

讓我們用括號來闡明編譯器是如何解釋的

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

即轉換到Node優先級低於. 運算符給你留下一個你嘗試調用.getSource() Object類型的表達式。 Object不包含getScene方法。 通過添加括號確保首先應用演員表:

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

另請注意,即使應用此修復程序后,您仍然會遇到會導致運行時異常的問題:您正在使用一些java.awt導入。 您需要用適當的 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">

我刪除了:

import java.awt.event.MouseEvent;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM