我在JavaFX中搜索弹出窗口的示例。 我有JavaFX应用程序,有一次我需要一个弹出窗口出现。这个弹出窗口需要一些复杂的输入,我需要处理和检查并返回主应用/窗口。
现在的问题是我找不到一个例子如何在一个JavaFX控制器类中调用现在的JavaFX弹出窗口?我只找到了如何使Dialog弹出窗口但我找不到基于JavaFX的新弹出窗口的例子(我看到一个解决方案,其中有两个窗口在paralel但我需要一个只在需要时创建)
你知道JavaFx自定义弹出窗口的这个例子吗?
答案 0 :(得分:6)
我想我明白你想要什么,这是一个(解决方法)示例:
<强> MainWindow.fxml 强>
undefined
<强> Popup.fxml 强>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="I'm the MAiN here">
<font>
<Font size="24.0" />
</font>
</Label>
<Label text="LETS POP THIS OUT">
<font>
<Font size="18.0" />
</font>
</Label>
<Button fx:id="popitBtn" mnemonicParsing="false" text="NOW">
<font>
<Font size="14.0" />
</font>
</Button>
<Label fx:id="resultLbl" text="I've got this (username: /Password: )">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</Label>
</children>
<padding>
<Insets bottom="40.0" left="40.0" right="40.0" top="40.0" />
</padding>
</VBox>
<强> AbstractController.java 强>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER" style="-fx-background-color: #e1c1c1;">
<children>
<Label text="POPUP WINDOW EXAMPLE" textFill="#752b2b">
<font>
<Font size="14.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER">
<children>
<Label prefWidth="70.0" text="Username" />
<TextField fx:id="usernameTF" promptText="John Doe" />
</children>
</HBox>
<HBox alignment="CENTER">
<children>
<Label prefWidth="70.0" text="Password" />
<PasswordField fx:id="passwordPF" promptText="*********" />
</children>
</HBox>
<HBox alignment="CENTER">
<children>
<Button fx:id="connectBtn" mnemonicParsing="false" text="Connect" />
</children>
</HBox>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
<强> MainApp.java 强>
public abstract class AbstractController {
protected MainApp main;
public void setMainApp(MainApp main) {
this.main = main;
}
}
<强> MainWindowController.java 强>
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainWindow.fxml"));
MainWindowController mainWindowController = new MainWindowController();
mainWindowController.setMainApp(this);
loader.setController(mainWindowController);
Parent layout = loader.load();
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.show();
}
public Stage getPrimaryStage() {
return primaryStage;
}
}
<强> PopupController.java 强>
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MainWindowController extends AbstractController implements Initializable {
@FXML private Button popitBtn;
@FXML private Label resultLbl;
@Override
public void initialize(URL url, ResourceBundle rb) {
resultLbl.setText("Lets get something in here");
popitBtn.setOnAction((event)->{
HashMap<String, Object> resultMap = showPopupWindow();
resultLbl.setText("I've got this (username: "+resultMap.get("username")
+" /Password: "+resultMap.get("password")+")");
});
}
private HashMap<String, Object> showPopupWindow() {
HashMap<String, Object> resultMap = new HashMap<String, Object>();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Popup.fxml"));
// initializing the controller
PopupController popupController = new PopupController();
loader.setController(popupController);
Parent layout;
try {
layout = loader.load();
Scene scene = new Scene(layout);
// this is the popup stage
Stage popupStage = new Stage();
// Giving the popup controller access to the popup stage (to allow the controller to close the stage)
popupController.setStage(popupStage);
if(this.main!=null) {
popupStage.initOwner(main.getPrimaryStage());
}
popupStage.initModality(Modality.WINDOW_MODAL);
popupStage.setScene(scene);
popupStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
return popupController.getResult();
}
}
答案 1 :(得分:0)
只需使用 -
FXMLLoader loader = new FXMLLoader(getClass()。getResource(&#34; fxml.file&#34;));
因为fxml文件会自动初始化相关类。