老规矩,先看效果图
点击新建时,创建一个弹出框。
实现过程
1、给新建按钮添加鼠标点击事件(onMouseClicked)
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1308.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.demo.plat.controller.ItemEditController">
<children>
<Label fx:id="createProject" alignment="BOTTOM_CENTER" layoutX="4.0" layoutY="-1.0" onMouseClicked="#onCreateProject" prefHeight="84.0" prefWidth="61.0" text="新建" />
</children>
</AnchorPane>
2、新建弹出框类及方法
public class CreateNewWindow {
public void start(Stage primaryStage) {
try {
// 弹出框对应的FXML文件
Parent parent = FXMLLoader.load(getClass().getResource(
"/CreateProject.fxml"));
// 设置弹出框大小
Scene scene = new Scene(parent, 850, 600);
primaryStage.setScene(scene);
// 设置弹出框标题
primaryStage.setTitle("基础信息配置");
// 设置弹出框大小是否可变
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(null);
} catch (IOException e) {
e.printStackTrace();
}
// 展示弹出框
primaryStage.show();
}
}
3、给鼠标点击对应的方法调用弹出框的方法(注意方法名与fxml中onMouseClicked对应方法名保持一致。这个类就是fxml中fx:controller指向的类)
@FXMLController
public class ItemEditController implements Initializable {
@FXML
public void onCreateProject() {
// 调用弹出方法
new CreateNewWindow().start(new Stage());
}
}