用FXML同时显示两个JavaFX窗口(最简单的方式)
2018-01-16 17:49
2361 查看
Preface
JavaFX显示多窗口其实是非常简单的,需要用两个FXML即可,不用像网上其他人弄的那么麻烦。环境:
IDEASceneBuilder V9.0.0
思路
只需要在start()函数里面事先
primaryStage一样的东西即可。
如下代码:(FXML见后面附)
Main.java
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 600, 400)); primaryStage.show(); Stage anotherStage = new Stage(); Parent anotherRoot = FXMLLoader.load(getClass().getResource("sample.fxml")); Scene anotherScene = new Scene(anotherRoot); anotherStage.setTitle("Another Window"); anotherStage.setScene(anotherScene); anotherStage.show(); } public static void main(String[] args) { launch(args); } }
运行结果
附
main.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main"> <children> <Button layoutX="310.0" layoutY="214.0" mnemonicParsing="false" text="Main Button" /> <Label alignment="CENTER" layoutX="142.0" layoutY="115.0" prefHeight="51.0" prefWidth="70.0" text="Main" textAlignment="CENTER"> <font> <Font size="19.0" /> </font> </Label> </children> </AnchorPane>
sample.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.RowConstraints?> <GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9" fx:controller="sample.Controller"> <columnConstraints> <ColumnConstraints /> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> </rowConstraints> <children> <AnchorPane prefHeight="329.0" prefWidth="369.0" GridPane.columnIndex="1"> <children> <Button layoutX="201.0" layoutY="180.0" mnemonicParsing="false" text="Test" /> </children> </AnchorPane> </children> </GridPane>
相关文章推荐
- PCL同时在一个窗口中显示两个(多个)点云
- Eclipse同时显示两个编辑窗口
- Eclipse同时显示两个编辑窗口
- Eclipse同时显示两个编辑窗口
- Eclipse快捷键:同时显示两个一模一样的代码窗口
- win32 窗口 两种方式显示字符串
- 如何让win7桌面显示两个并列窗口边看视频边聊天
- 如何关闭WPS文字的选项卡显示方式?如何实现多窗口显示?
- JavaFX 表格数据显示简单Demo
- C/C++ 通过管道的方式调用Windows ping命令 且 不显示黑窗口(cmd 窗口)
- Split Pannel同时显示两个不同的网页
- IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
- Jquery实现显示和隐藏的4种简单方式
- JAVA--编写一个JFrame,标题为“计算的窗口”,在该窗口中组件的布局是FlowLayout。窗口中添加两个文本区,当我们在一个文本区中输入若干个数时,另一个文本区同时对输入的数进行求和运算并求
- 窗口上提供四个文本框和一个按钮,幼儿可以在前三个文本框中输入两个运算数和运算符号,当点击按钮时在第四个文本框中显示结果。
- MFC多文档应用程序同时显示两个视图
- 在DirectX 全屏独占 方式下显示对话框和任意窗口
- xcode 两个窗口 上下显示
- 在ios中举个简单的protocol例子,关于两个类用协议方式传值。
- 有关选中listView中一行,在特定窗口中显示相应信息的两个事件说明