-1
我在學習如何讓一個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);
}
}
你想顯示一個彈出窗口或只是swich場景?你問如何顯示彈出窗口,但你的代碼似乎只是在初級階段swich場景。將「Stage」的所有者設置爲自己並不合理。如果你需要在初級階段顯示彈出窗口,那麼在'OpenScene2'方法中創建新的'Stage',設置它的模態,所有者,然後設置'show()'。 – MBec
感謝MBec,我改變了我的OpenScene2方法,正如你所建議的那樣,它的工作原理! – Luft
順便說一句,我不知道你是否是標記我的問題的人,但無論誰做了什麼,我是否可以指出我的問題沒有任何問題。正如我剛開始所說的,我正在試着去學習這些東西。如果我知道該怎麼做,我不會問,對嗎?通過標記合法的問題,你正在驗證廣泛的意見,這個網站是由不友善的純粹勢利克斯控制的。 'nuff說。 – Luft