javafx01 javafx生命周期、窗口设置、窗口权限、Platform、Screen、Group

一、第一个javafx小程序

package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application
{
    public static void main( String[] args )
    {
//        第一个参数就是继承Application的类
        launch(Main.class,args);
        System.out.println( "Hello World!" );
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.show();
        System.out.println("hello");
    }
}

也可以分离

package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class Main
{
    public static void main( String[] args )
    {
        Application.launch(Launch.class,args);
    }

}

package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class Launch extends Application {

    public void start(Stage primaryStage) throws Exception {
        primaryStage.show();
    }
}

二、javafx程序生命周期

注意:start必写

package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class Launch extends Application {
//    先初始化 再开始 再关闭
//      初始化
    @Override
    public void init() throws Exception {
        super.init();
        System.out.println("初始化了");
    }
//    窗口打开的时候
     public void start(Stage primaryStage) throws Exception {
        primaryStage.show();
         System.out.println("开始了");
    }


//  关闭的时候
    @Override
    public void stop() throws Exception {
        super.stop();
        System.out.println("关闭了");
    }
}

三、javafx窗口的相关设置

package org.example;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Launch extends Application {
//    窗口打开的时候
     public void start(Stage primaryStage) throws Exception {
         final Stage stage = new Stage();
//         设置标题
         stage.setTitle("My程序");
//         设置图标
         stage.getIcons().add(new Image("/image/img1.png"));
//         stage.setIconified(false); //设置打开即最小化
//
//         stage.setMaximized(true); //设置打开即最大化
//
//         设置长度和宽度
         stage.setWidth(500);
         stage.setHeight(500);
//
//         不允许调窗口大小
//         stage.setResizable(false);

//         设置宽度最小值
         stage.setMinWidth(200);
         stage.setMinHeight(200);
//         设置宽度和高度最大值
         stage.setMaxHeight(1000);
         stage.setMaxWidth(1000);

//         获取长度和宽度  如果没设置宽和高 在show才可以获得
         System.out.println("宽度:"+stage.getWidth());
         System.out.println("高度:"+stage.getHeight());

//         监听窗口大小的变化
         stage.widthProperty().addListener(new ChangeListener<Number>() {
             public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                 System.out.println(oldValue);
             }
         });

//         设置全屏
//         stage.setFullScreen(true);
//         全屏必须设置setScene  同时也可以实现 容器随着窗口改变
         stage.setScene(new Scene(new Group()));
//         设置窗口透明度
//         stage.setOpacity(0.1);
//         窗口置顶
         stage.setAlwaysOnTop(true);
//         设置初始的位置
         stage.setX(200);
         stage.setY(200);
//         窗口移动的时候的监听事件
         stage.yProperty().addListener(new ChangeListener<Number>() {
             public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                 System.out.println(oldValue);
             }
         });
//         设置窗口样式
         stage.initStyle(StageStyle.UNIFIED);

//         展示窗口
         stage.show();

//        stage.close();//关闭窗口
         System.out.println("开始了");

//         平台工具 可以直接调用
         Platform.exit();
    }
}


四、窗口的权限

package org.example;

import javafx.application.Application;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Launch2 extends Application {
    public void start(Stage primaryStage) throws Exception {
        Stage stage1 = new Stage();
        stage1.setTitle("1");
        stage1.show();
        Stage stage2 = new Stage();
        stage2.setTitle("2");
        stage2.initOwner(stage1);
//        设置不允许操作其他窗口initOwner 表示不可以操作的窗口
        stage2.initModality(Modality.WINDOW_MODAL);
        stage2.show();
        Stage stage3 = new Stage();
        stage3.setTitle("3");

        stage3.show();
    }
}

五、Platform工具类

package org.example;

import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.stage.Stage;

public class Launch3 extends Application {
    public void start(Stage primaryStage) throws Exception {
//        在加载的过程中 可以先运行一个线程
        Platform.runLater(new Runnable() {
            public void run() {
                System.out.println("更新");

                int i=0;
                while (i<10){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(i);
                    i++;
                }
            }
        });
//        设置窗口关掉仍继续运行
        Platform.setImplicitExit(false);
//        必须调用exit()才可以退出
        Platform.exit();
        primaryStage.show();
//        判断参数 是否支持对应服务
        System.out.println(Platform.isSupported(ConditionalFeature.FXML));
        System.out.println("接着操作");

    }
}

六、Screen类

package org.example;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Launch4 extends Application {
    public void start(Stage primaryStage) throws Exception {
        Screen screen = Screen.getPrimary();
//        获得窗口的宽和高
        Rectangle2D bounds = screen.getBounds();
//        获得用户的可视范围
        Rectangle2D visualBounds = screen.getVisualBounds();
        System.out.println(bounds.getMinY());
        System.out.println(bounds.getMinX());
        System.out.println(bounds.getMaxX());
        System.out.println(bounds.getMaxY());
        System.out.println(visualBounds.getMinX());
        System.out.println(visualBounds.getMinX());
        System.out.println(visualBounds.getMaxX());
        System.out.println(visualBounds.getMaxY());
//      获取dpi 像素每英寸
        double dpi = screen.getDpi();
        System.out.println(dpi);
        Platform.exit();
    }
}

package org.example;

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.net.URL;

public class Launch5 extends Application {
    public void start(Stage primaryStage) throws Exception {
//        stage包含scene scene包含group group上加组件
        Button button = new Button();
        Group group = new Group();
//        点击按钮访问网页
        button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                HostServices host=getHostServices();
                host.showDocument("www.baidu.com");
            }
        });
//        往group上添加 button
        group.getChildren().add(button);
        Scene scene = new Scene(group);
//        改变scene光标样式
//        scene.setCursor(Cursor.CLOSED_HAND);
//        还可以自定义光标
        URL url=getClass().getResource("/image/img1.png");
        String path = url.toExternalForm();
        scene.setCursor(Cursor.cursor(path));
        primaryStage.setScene(scene);
        primaryStage.setWidth(500);
        primaryStage.setHeight(500);



        primaryStage.show();
    }
}

七、Group类

package org.example;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.util.List;

public class Launch6 extends Application {
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("b1");
        Button b2 = new Button("b2");
        Button b3 = new Button("b3");
//        调整组件的位置
        b2.setLayoutX(200);
        b3.setLayoutX(400);

        Group group = new Group();
//      将组件全部加入
        group.getChildren().addAll(b1,b2,b3);
//        不允许自动调整组件
//        group.setAutoSizeChildren(false);
//       设置透明度
//        group.setOpacity(0.5);
//        判断对应的位置有没有组件
        System.out.println(group.contains(10, 10));
//        显示所有的孩子
        for (Object o : group.getChildren().toArray()) {
            System.out.println((Button)o);
        }

//      删除组件
        group.getChildren().remove(b1);
        Scene scene = new Scene(group);

        primaryStage.setTitle("My程序");
        primaryStage.setScene(scene);
        primaryStage.setWidth(1000);
        primaryStage.setHeight(800);
        primaryStage.show();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值