JavaFX弹出窗口不工作
问题描述:
我在学习如何让一个JavaFX弹出窗口从菜单项点击打开。我终于打开了一扇新窗口,但我似乎无法使它成为模态。这里是我的代码:JavaFX弹出窗口不工作
场景1个FXML:
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_1Controller">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<MenuBar fx:id="menuBar" layoutX="40.0" layoutY="27.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem fx:id="MenuItemOpenScene2" mnemonicParsing="false" onAction="#OpenScene2" text="Open Scene 2" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
弹出FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_2Controller">
</AnchorPane>
场景1个控制器:
package sceneswitchtest;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class Scene_1Controller {
@FXML
private Button button;
@FXML
private Label label;
@FXML
private MenuBar menuBar;
@FXML
private MenuItem MenuItemOpenScene2;
@FXML
void OpenScene2(ActionEvent event) throws IOException {
Stage appStage = (Stage) ((Node) menuBar).getScene().getWindow();
Parent settingsParent = FXMLLoader.load(this.getClass().getResource("Scene_2.fxml"));
Scene settingsScene = new Scene(settingsParent);
appStage.setScene(settingsScene);
// appStage.initModality(Modality.WINDOW_MODAL);
// appStage.initOwner(menuBar.getScene().getWindow());
appStage.show();
}
@FXML
void handleButtonAction(ActionEvent event) throws IOException {
}
}
注:在上面的代码,该行评论失败以及他使用Modality.APPLICATION_MODAL
场景2(弹出)控制器:
package sceneswitchtest;
public class Scene_2Controller {
}
主要方法:
包sceneswitchtest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author eric
*/
public class SceneSwitchTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene_1.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
答
这是我为了让弹出窗口工作而做的代码更改。
@FXML
void OpenScene2(ActionEvent event) throws IOException {
Parent settingsParent = FXMLLoader.load(this.getClass().getResource("/sceneswitchtest/Scene_2.fxml"));
Scene settingsScene = new Scene(settingsParent);
Stage popup = new Stage();
popup.setScene(settingsScene);
popup.initModality(Modality.WINDOW_MODAL);
popup.initOwner(menuBar.getScene().getWindow());
popup.show();
}
我想感谢MBec指引我朝着正确的方向前进。
你想显示一个弹出窗口或只是swich场景?你问如何显示弹出窗口,但你的代码似乎只是在初级阶段swich场景。将“Stage”的所有者设置为自己并不合理。如果你需要在初级阶段显示弹出窗口,那么在'OpenScene2'方法中创建新的'Stage',设置它的模态,所有者,然后设置'show()'。 – MBec
感谢MBec,我改变了我的OpenScene2方法,正如你所建议的那样,它的工作原理! – Luft
顺便说一句,我不知道你是否是标记我的问题的人,但无论谁做了什么,我是否可以指出我的问题没有任何问题。正如我刚开始所说的,我正在试着去学习这些东西。如果我知道该怎么做,我不会问,对吗?通过标记合法的问题,你正在验证广泛的意见,这个网站是由不友善的纯粹势利克斯控制的。 'nuff说。 – Luft