我正在搜索JavaFX中弹出窗口的示例。我有JavaFX应用程序,有一次我需要一个弹出窗口出现。这个弹出窗口需要一些复杂的输入,我需要处理和检查并返回主应用程序/窗口。
现在的问题是,我在任何地方都找不到一个示例,说明如何在一个JavaFX控制器类中调用Now JavaFX弹出窗口?我只找到了examle如何创建对话框弹出窗口,但找不到基于JavaFX的新弹出窗口示例(我看到了一个解决方案,其中并行有两个窗口,但我只需要在需要时创建一个)。
您知道JavaFx自定义弹出窗口的这样的例子吗?
哈立德·萨布的回答很好。如果使用sceneBuilder创建fxml文件,请不要设置setController(在函数showPopupWindow()中)。
只是使用-
FXMLLoader=new FXMLLoader(getClass(). getResources("fxml.file"));
因为fxml文件会自动初始化相关类。
我想我知道你想要什么,这里有一个(解决方案)示例:
github 上的源代码
截图
MainWindow.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>
Popup.fxml
<?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>
摘要控制器.java
public abstract class AbstractController {
protected MainApp main;
public void setMainApp(MainApp main) {
this.main = main;
}
}
MainApp.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;
}
}
MainWindowController.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();
}
}
PopupController.java
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class PopupController extends AbstractController implements Initializable {
@FXML private TextField usernameTF;
@FXML private PasswordField passwordPF;
@FXML private Button connectBtn;
private Stage stage = null;
private HashMap<String, Object> result = new HashMap<String, Object>();
@Override
public void initialize(URL url, ResourceBundle rb) {
connectBtn.setOnAction((event)->{
result.clear();
result.put("username", usernameTF.getText());
result.put("password", passwordPF.getText());
closeStage();
});
}
public HashMap<String, Object> getResult() {
return this.result;
}
/**
* setting the stage of this view
* @param stage
*/
public void setStage(Stage stage) {
this.stage = stage;
}
/**
* Closes the stage of this view
*/
private void closeStage() {
if(stage!=null) {
stage.close();
}
}
}
问题内容: avaFX 2颜色选择器具有一个按钮,它会弹出一个颜色选择器窗格,如下所示: JavaFX 2颜色选择器 我想做类似的事情,因为我希望自定义窗格在单击按钮时弹出,而在单击其他按钮时消失(在我的情况下,是一些图像缩略图)。实现此目标的最佳方法是什么?我应该使用ContextMenu并以某种方式将窗格添加到MenuItem,还是应该查看其他内容? 问题答案: 当前的JavaFX 2.2 A
JavaFX 2颜色选择器有一个按钮,可以弹出一个颜色选择器窗格,如下所示: 我想做一些类似的事情,我想在点击按钮时弹出一个自定义窗格,然后在点击其他东西时消失(在我的例子中,是一些图像缩略图)。实现这一目标的最佳方式是什么?我应该使用ContextMenu并以某种方式将窗格添加到MenuItem,还是应该查看其他内容?
本文向大家介绍WPF弹出自定义窗口的方法,包括了WPF弹出自定义窗口的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了WPF弹出自定义窗口的方法。分享给大家供大家参考,具体如下: 测试环境: [1]VS2010SP1 [2]WPF(.NET Framework 4)项目 内容简介 WPF工程如何弹出自定义窗口 第一步:自定义个窗口 为当前项目新添个Window项,XAML部份的代码略,
我正在使用Kendo网格,其中有几个列用于概览行数据。当用户单击添加/编辑按钮时,弹出窗口将显示一些附加数据,其中包括一些复选框。 我在将复选框与当前MVVM模型绑定时遇到问题,因为在添加新行时,Kendo将模型视为变量,而不是数组。这会导致在选中一个复选框(单击)时选中多个复选框。在看了剑道MVVM之后,我打算得到当前弹出窗口的MVVM模型,以便操作一些数据,但没有成功。因此,我将在以下方面寻求
dialog组件使用visible这个prop开控制显示,监听visible赋值给visibleMe v-if=“visibleMe”的方式打开关闭弹窗 关闭的时候令visibleMe=false,但是因为visible是prop,不可以在这个组件内更改,所以visible还是true 所以下次调用它的组件再把visible改成true的时候,并不会触发watch 怎么通过只改dialog组件的代
所以我只是想知道是否有人知道在JavaFX 8中关闭弹出窗口的正确方法。例如,如果我的弹出窗口上有一个取消按钮,当按下取消按钮时,我应该使用什么方法来摆脱弹出窗口?我目前只是使用隐藏()方法。这让我有点害怕,因为我不确定弹出窗口是否在后台某处徘徊并且仍然需要关闭。但是,当我在这里查看java文档时,我没有看到任何关闭()方法,我也没有在我的IDE自动完成中看到关闭()方法。不过,我确实在文档中看到