生命周期
- init() - 非UI线程,不能处理UI
- start() - 开始,处于UI线程(一般情况下,程序只需要重写该生命周期即可
stop() - 结束,处于UI线程 ```java public class Main extends Application {
@Override public void init() throws Exception {
super.init();
}
@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setTitle("Hello World");primaryStage.setScene(new Scene(root, 300, 275));primaryStage.show();}@Overridepublic void stop() throws Exception {super.stop();}public static void main(String[] args) {launch(args);}
}
<a name="aV3Pr"></a>## 启动窗口及关闭```java@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setScene(new Scene(root, 300, 275));primaryStage.show();//启动primaryStage.close();//关闭}
注:一般情况下,窗口关闭了,且没有其他任务,程序就结束了,如果想要窗口关闭,程序不结束可以设置
Platform.setImplicitExit(false);//在start中设置
注:设置以上方法后,想要退出,可以调用
Platform.exit();
设置窗口大小
最小化及最大化
primaryStage.setIconified(true);//设置最小化primaryStage.setMaximized(true);//设置最大化
设置全屏
@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setFullScreen(true);primaryStage.setScene(new Scene(root));//root也可以new Group()primaryStage.show();}
设置指定大小
@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setScene(new Scene(root, 300, 275));primaryStage.show();}
监听窗口大小
获取宽高 ```java primaryStage.show();//需要在show之后才能获取到宽高
//获取宽高 primaryStage.getHeight(); primaryStage.getWidth();
- 监听宽高变化```java//监听宽度实时变化primaryStage.widthProperty().addListener(new ChangeListener<Number>() {@Overridepublic void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {System.out.println("当前宽度为:"+newValue.doubleValue());}});primaryStage.heightProperty().addListener(new ChangeListener<Number>() {@Overridepublic void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {System.out.println("当前高度为:"+newValue.doubleValue());}});
设置窗口图标
primaryStage.getIcons().add(new Image("file:E:\\图\\表情包\\猫\\还有这种操作.jpg"));//这里也可以是网络url,如果是本地文件,需要添加file:的前缀
设置窗口透明度
primaryStage.setOpacity(0.5);
设置窗口强制置顶
primaryStage.setAlwaysOnTop(true);

相当于该功能。
设置窗口的坐标(左上角的坐标)
primaryStage.setX(0);primaryStage.setY(0);
注:上例的作用为显示在桌面的左上角
多窗口创建及其启动模式
模式
public class Main2 extends Application {@Overridepublic void start(Stage primaryStage) throws Exception {Stage s1 = new Stage();s1.setTitle("s1");//纯白背景和平台装饰s1.initStyle(StageStyle.DECORATED);s1.show();Stage s2 = new Stage();s2.setTitle("s2");//透明背景且没有装饰s2.initStyle(StageStyle.TRANSPARENT);s2.show();Stage s3 = new Stage();s3.setTitle("s3");//纯白背景,无装饰。s3.initStyle(StageStyle.UNDECORATED);s3.show();Stage s4 = new Stage();s4.setTitle("s4");//纯白背景和最少平台装饰s4.initStyle(StageStyle.UNIFIED);s4.show();Stage s5 = new Stage();s5.setTitle("s5");//一个统一标准的舞台s5.initStyle(StageStyle.UTILITY);s5.show();// 终止JavaFX应用程序Platform.exit();}public static void main(String[] args) {launch();}}
模态
Stage s3 = new Stage();//当该窗口在启动时,无法操作该应用的其他窗口,除非先关闭该窗口,s3.initModality(Modality.APPLICATION_MODAL);s3.initOwner(s2);// 阻止事件传递到所有者的窗口//s3.initModality(Modality.WINDOW_MODAL);s3.setTitle("s3");s3.show();
UI线程
@Overridepublic void start(Stage primaryStage) throws Exception{new Threath(new Runnable() {@Overridepublic void run() {//切回UI线程更新UIPlatform.runLater(new Runnable() {@Overridepublic void run() {//处于UI线程,不能做耗时任务,更新UI}});}});).start()}
检查当前平台是否支持某特性
// 检查是否支持某种特性System.out.println(Platform.isSupported(ConditionalFeature.SCENE3D))
添加控件
@Overridepublic void start(Stage primaryStage) throws Exception{URL url = getClass().getClassLoader().getResource("bin/hello");System.out.println(url.toExternalForm());TextArea tv = new TextArea();//文本框tv.setText("欸哈哈哈哈哈你");tv.setLayoutX(100);//设置横坐标Button btn = new Button();btn.setPrefWidth(100);//按钮的宽btn.setPrefHeight(50);//按钮的高Group group = new Group();// group.getChildren().add(btn);// group.getChildren().add(tv);group.getChildren().addAll(tv,btn);Scene scene = new Scene(group);scene.setCursor(Cursor.MOVE);//设置鼠标图标类型,可不设置primaryStage.setScene(scene);primaryStage.setTitle("标题栏");primaryStage.show();//启动}

