JavaFX(二)了解窗口和场景类


 

JavaFX(一)入门介绍


 

1各类窗口,窗口之间的交互

五种窗口类型

DECORATED,
UNDECORATED,
TRANSPARENT,
UTILITY,
UNIFIED

 1 package com.fxUnit03;
 2 
 3 import javafx.application.Application;
 4 import javafx.stage.Modality;
 5 import javafx.stage.Stage;
 6 import javafx.stage.StageStyle;
 7 
 8 
 9 /**
10  *  @Name FX learning unit 3
11  *  @Description 各类窗口,窗口之间的交互
12  *  @Author Fancy
13  *  @return 2022/6/13
14  *  @Version 1.0
15  */
16 public class fxTest3 extends Application {
17     public static void main(String[] args) {
18         launch(args);
19     }
20 
21     @Override
22     public void start(Stage primaryStage) throws Exception {
23         //对话框,是/否?
24         Stage s1 = new Stage(StageStyle.UTILITY);
25         s1.setTitle("close");
26 
27         //设置为APPLICATION_MODAL则必须先关掉该窗口,才能使用其它窗口
28         s1.initModality(Modality.APPLICATION_MODAL);
29         s1.setWidth(200);
30         s1.setHeight(200);
31         s1.setX(800);
32         s1.setY(500);
33 
34         //常用窗口类型
35         Stage s2 = new Stage(StageStyle.DECORATED);
36         s2.setTitle("s2");
37         //s2窗口拥有s1
38         s2.initOwner(s1);
39         s2.initModality(Modality.WINDOW_MODAL);
40         s1.setAlwaysOnTop(true);
41 
42         s2.show();
43         s1.show();
44     }
45 }

2 窗口的线程名

start方法线程名JavaFX Application Thread
结束后线程名JavaFX Application Thread
线程名JavaFX Application Thread

 1 package com.fxUnit04;
 2 
 3 import javafx.application.Application;
 4 import javafx.application.Platform;
 5 import javafx.stage.Stage;
 6 import javafx.stage.StageStyle;
 7 
 8 /**
 9  *  @Name
10  *  @Description 多线程
11  *  @Author Fancy
12  *  @return 2022/6/14
13  *  @Version 1.0
14  */
15 public class fxTest4 extends Application {
16     public static void main(String[] args) {
17         launch(args);
18     }
19     @Override
20     public void start(Stage primaryStage) throws Exception {
21         System.out.println("start方法线程名"+Thread.currentThread().getName());
22 
23         Stage s1 = new Stage(StageStyle.UNIFIED);
24 
25         Platform.runLater(new Runnable() {
26             @Override
27             public void run() {
28                 System.out.println("线程名"+Thread.currentThread().getName());
29                 int i = 1;
30                 while (i <= 10){
31                     try {
32                         Thread.sleep(1000);
33                     } catch (InterruptedException e) {
34                         e.printStackTrace();
35                     }
36                     System.out.println("i = "+i);
37                     i = i+1 ;
38 
39                 }
40             }
41         });
42         s1.setTitle("test4");
43         s1.show();
44 
45         System.out.println("结束后线程名"+Thread.currentThread().getName());
46     }
47 }

3 场景类Scene和Group组

package com.fxUnit06;

import javafx.application.Application;
import javafx.application.HostServices;
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 fxTest6 extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        //打开网页
        HostServices host = getHostServices();
        host.showDocument("https://www.cnblogs.com/fancy2022/");
        //获取本地图片路径
        URL url = getClass().getClassLoader().getResource("img/bv_Logo.png");
        String path = url.toExternalForm();

        Button button = new Button("登录");
        button.setPrefSize(40,40);
        //布局类管理button
        Group group = new Group();
        //添加组件
        group.getChildren().add(button);
        Scene scene = new Scene(group);

        //在按钮上设置鼠标变型
        button.setCursor(Cursor.CLOSED_HAND);

        //将scene关联到主窗口上
        primaryStage.setScene(scene);

        primaryStage.setTitle("fxTest6");
        primaryStage.setHeight(800);
        primaryStage.setWidth(1000);


        primaryStage.show();



    }
}
package com.fxUnit07;

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


/**
 *  @Name test7
 *  @Description group
 *  @Author Fancy
 *  @return 2022/6/22
 *  @Version 1.0
 */
public class fxTest7 extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("按钮1");
        Button b2 = new Button("按钮2");
        Button b3 = new Button("按钮3");

/*        //设置按钮的大小
        b1.setPrefSize(50,30);
        b2.setPrefSize(50,30);
        b3.setPrefSize(50,30);*/


        //设置按钮的位置
        b1.setLayoutX(0);
        b2.setLayoutX(100);
        b3.setLayoutX(200);

        Group group = new Group();

        group.getChildren().addAll(b1,b2,b3);

        //操作三个子节点
        Object[] obj = group.getChildren().toArray();
        for (Object o : obj) {
            Button bu = (Button) o;
            bu.setPrefSize(50,50);
        }

        //监听
        group.getChildren().addListener(new ListChangeListener<Node>() {
            @Override
            public void onChanged(Change<? extends Node> c) {
                System.out.println("当前子组件数量= "+c.getList().size());
            }
        });

        //设置group的透明度
        group.setOpacity(0.8);

        //判断组件有无子组件
        System.out.println(group.contains(0,0));

        Scene scene = new Scene(group);
        //将scene关联到主窗口上
        primaryStage.setScene(scene);

        primaryStage.setTitle("fxTest6");
        primaryStage.setHeight(800);
        primaryStage.setWidth(1000);


        primaryStage.show();
    }
}

代码来源:B站UP:Aimls

https://space.bilibili.com/5096022/channel/collectiondetail?sid=210809

搜索

复制

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/277247.html

(0)
上一篇 2022年7月27日
下一篇 2022年7月27日

相关推荐

发表回复

登录后才能评论