hellowWorld

 

package test;

/**
 * @author major_s
 * @date 2020/6/13 - 11:21
 **/

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

public class helloWorld extends Application {

    public static void main(String[] args) {
        System.out.println("hello world");
        launch(args);
    }

    public void start(Stage stage){
        stage.show();
    }
}

application的启动方式和生命周期

 

package fx.com;

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

/**
 * @author major_s
 * @date 2020/6/13 - 12:09
 **/
public class Main4 extends Application {
    public static void main(String[] args) {
        System.out.println("main ="+ Thread.currentThread().getName());
           launch(args);
    }

    @Override
    public void init() throws Exception {
        super.init();
        System.out.println("init="+ Thread.currentThread().getName());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        System.out.println("start="+ Thread.currentThread().getName());
        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        System.out.println("stop="+ Thread.currentThread().getName());
    }
}

初步认识stage窗口

 

package lesson;

import javafx.application.Application;
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.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/13 - 12:46
 **/
public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("江苏慧明科技有限公司");
        primaryStage.getIcons().add(new Image("/icon/icon.png"));
        // primaryStage.setIconified(true); //设置最小化
        // primaryStage.setMaximized(true);  // 设置最大化
        // 设置窗口大小
         primaryStage.setHeight(500);
         primaryStage.setWidth(500);

        // primaryStage.setResizable(false); // 窗口大小是否可变



        // 设置全屏

        primaryStage.setFullScreen(true);
        primaryStage.setScene(
                new Scene(new Group())
        );

         primaryStage.show();


         // 设置最大、最小的窗口大小

        // primaryStage.setMaxHeight(1000);
        //primaryStage.setMinHeight(10);

        // primaryStage.close();

        // 获取窗口宽度
        //primaryStage.getWidth();

        // 动态获得窗口大小

        primaryStage.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                System.out.println(newValue.doubleValue());
            }
        });




    }
}

进一步认识stage窗口,模式,模态

 

package lesson4;

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

/**
 * @author major_s
 * @date 2020/6/13 - 13:17
 **/
public class Main extends Application {

    public static void main(String[] args) {

    }
    @Override
    public void start(Stage primaryStage) throws Exception {


        // 设置透明度
        primaryStage.setOpacity(0.5);

        // 窗口置顶  屏蔽广告
        //primaryStage.setAlwaysOnTop(true);

        // 设置窗口起始位置
        primaryStage.setX(100);
        primaryStage.setY(100);

        // 动态监听窗口起始位置



    }
}

package lesson4;

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

/**
 * @author major_s
 * @date 2020/6/13 - 13:23
 **/
public class Main2 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public 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();


        // 关闭所有

       // Platform.exit();
    }
}

模态框:

package lesson4;

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

/**
 * @author major_s
 * @date 2020/6/13 - 13:34
 **/
public class Main3 extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Stage s1 = new Stage();

        s1.setTitle("s1");


        Stage s2 = new Stage();
        s2.initOwner(s1);
        s1.initModality(Modality.WINDOW_MODAL);
        s2.setTitle("s2");


        Stage s3 = new Stage();
        s3.initModality(Modality.APPLICATION_MODAL);
        s3.setTitle("s3");

        s1.show();
        s2.show();
        s3.show();


    }
}

platform类的使用

 

package lesson5;

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

/**
 * @author major_s
 * @date 2020/6/13 - 13:47
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        System.out.println(" start Thread's name"+Thread.currentThread().getName());
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("run Thread's name"+Thread.currentThread().getName());
                System.out.println("sss");
            }
        });

        System.out.println("kkkk");
    }
}

package lesson5;

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

/**
 * @author major_s
 * @date 2020/6/13 - 13:52
 **/
public class Main2 extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
//        // 不会这样
//        Platform.setImplicitExit(false);
//        primaryStage.show();
//        Platform.exit();
         // 查看支持
     //   System.out.println(Platform.isSupported(ConditionalFeature.SCENE3D));

    }
}

screen类

 

package lesson6;


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

import java.awt.*;

/**
 * @author major_s
 * @date 2020/6/13 - 13:59
 **/
public class Main extends Application {


    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Screen screen = Screen.getPrimary();
        Rectangle2D rec1 = screen.getBounds();
        Rectangle2D rec2 = screen.getVisualBounds();


        // 下面是屏幕宽高和坐标
        System.out.println("左上角x="+rec1.getMinX()+"   "+"左上角y="+rec1.getMinY());
        System.out.println("右下角x="+rec1.getMaxX()+"   "+"右上角y="+rec1.getMaxY());

        // 下面是屏幕宽高和坐标
        System.out.println("左上角x="+rec2.getMinX()+"   "+"左上角y="+rec2.getMinY());
        System.out.println("右下角x="+rec2.getMaxX()+"   "+"右上角y="+rec2.getMaxY());
        Platform.exit();


    }
}

scene类,和两个小知识

 

hellowWorld_ide

package lesson7;

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javafx.scene.control.Button;

import java.net.URL;

/**
 * @author major_s
 * @date 2020/6/15 - 21:02
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        // 指定游标为某图片
//        URL url=getClass().getClassLoader().getResource("");
//        String path=url.toExternalForm();
        // 打开某个网页
        HostServices host=getHostServices();
        host.showDocument("www.baidu.com");
        
        Button button = new Button("按钮");
        button.setCursor(Cursor.E_RESIZE);
        Group group= new Group();

        group.getChildren().add(button);
        Scene scene= new Scene(group);
        scene.setCursor(Cursor.CLOSED_HAND);
        // scene.setCursor(Cursor.cursor(path));


        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);

        primaryStage.show();
    }
}

Group容器的使用

 

package lesson8;

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

import java.sql.BatchUpdateException;

/**
 * @author major_s
 * @date 2020/6/15 - 21:19
 **/
public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1= new Button("b1");
        Button b2= new Button("b2");
        Button b3= new Button("b3");

        b1.setLayoutX(0);
        b1.setLayoutY(0);

        b1.setPrefHeight(66);
        b1.setPrefWidth(66);

        b2.setLayoutX(200);
        b2.setLayoutY(10);

        b3.setLayoutX(400);
        b3.setLayoutY(10);


        Group group =new Group();


        group.setOpacity(0.5);

//        group.contains(0,0);

//        group.getChildren().add(b1);
//        group.getChildren().add(b2);
//        group.getChildren().add(b3);
        group.getChildren().addAll(b1,b2,b3);
//        group.getChildren().remove(0);
//        group.getChildren().clear();


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




        Object[] obj= group.getChildren().toArray();
        System.out.println(obj.length);



        for (Object o : obj){
            Button bu =(Button)o;
            bu.setPrefWidth(100);
            bu.setPrefHeight(100);
        }
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Button b4 = new Button("b4");
                group.getChildren().add(b4);
            }
        });


        Scene scene = new Scene(group);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }
}

Button按钮以及简单介绍设置背景颜色和外边框等问题

 

package lesson9;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/15 - 21:57
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button();
        b1.setText("B1");
        b1.setLayoutY(100);
        b1.setLayoutX(100);

        b1.setPrefHeight(200);
        b1.setPrefWidth(200);
        b1.setFont(Font.font("sans-serif",40));
//        BackgroundFill bgf = new BackgroundFill(Paint.valueOf("#8FBC8F"),new CornerRadii(20),new Insets(10)); // Inserts 上下左右的边距
//        Background bg = new Background(bgf);
//        b1.setBackground(bg);
//        BorderStroke bos = new BorderStroke(Paint.valueOf("#8A2BE2"), BorderStrokeStyle.SOLID,new CornerRadii(20),new BorderWidths(20));
//        Border bo = new Border(bos);
//        b1.setBorder(bo);


        b1.setStyle("-fx-background-color: #7CCD7C;"
                +"-fx-background-radius: 20;"
                +"-fx-text-fill: #5CACEE;"



        );

        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Button bu = (Button)event.getSource();
                System.out.println("bi's text:"+bu.getText());
            }
        });





        Group group = new Group(b1);
        Scene scene = new Scene(group);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }
}

双击事件和检测键盘按键

 

package lesson10;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import javax.sound.midi.SoundbankResource;


/**
 * @author major_s
 * @date 2020/6/15 - 21:57
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("B1");
        b1.setLayoutX(100);
        b1.setLayoutY(100);

        b1.setPrefWidth(400);
        b1.setPrefHeight(200);

        b1.setFont(Font.font(40));

        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("B1的单击事件");
            }
        });

        b1.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //System.out.println("b1点击事件");
                System.out.println("鼠标按键"+event.getButton().name());

                if(event.getClickCount() == 2  && event.getButton().name().equals(MouseButton.PRIMARY.name())){

                    System.out.println("双击事件");
                }

            }
        });


        b1.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                System.out.println("按下"+event.getCode().getName());
                System.out.println("按下");

                if (event.getCode().getName().equals(KeyCode.A.getName())){
                    System.out.println("按下= "+event.getCode().getName());
                }
            }
        });

//        b1.setOnKeyReleased(new EventHandler<KeyEvent>() {
//            @Override
//            public void handle(KeyEvent event) {
//                System.out.println("释放"+event.getCode().getName());
//                System.out.println("释放");
//
//            }
//        });



        Group group = new Group(b1);
        Scene scene = new Scene(group);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }
}

设置快捷键

 

package lesson11;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/15 - 21:57
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("B1");
        b1.setLayoutX(100);
        b1.setLayoutY(100);

        b1.setPrefWidth(400);
        b1.setPrefHeight(200);
        b1.setFont(Font.font(40));

        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                play();
            }
        });


        Group group = new Group(b1);
        Scene scene = new Scene(group);

        // 第一种
        KeyCombination kc1 = new KeyCodeCombination(KeyCode.C,KeyCombination.ALT_DOWN,KeyCombination.CONTROL_DOWN);
        Mnemonic mnemonic1 = new Mnemonic(b1,kc1);
        scene.addMnemonic(mnemonic1);
//
//        //第二种
//
//        KeyCombination kc2 = new KeyCharacterCombination("O",KeyCombination.ALT_DOWN);
//        Mnemonic mnemonic2 = new Mnemonic(b1,kc2);
//        scene.addMnemonic(mnemonic2);
//
//        // 第三种
//
//        KeyCombination kc3 = new KeyCodeCombination(KeyCode.K,KeyCombination.SHIFT_DOWN,KeyCombination.CONTROL_DOWN);
//        Mnemonic mnemonic3 = new Mnemonic(b1,kc3);
//        scene.addMnemonic(mnemonic3);

        // 第四种Y
        KeyCombination kc4 = new KeyCodeCombination(KeyCode.Y,KeyCombination.SHORTCUT_DOWN);
        scene.getAccelerators().put(kc4, new Runnable() {
            @Override
            public void run() {
                play();
            }
        });


        
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }

    public static  void play(){
        System.out.println("play");
    }
}

输入框,密码框,标签,实用技术

 

package lesson12;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/16 - 10:29
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {




//        PasswordField ptext = new PasswordField();
//        ptext.setLayoutX(145);
//        ptext.setLayoutY(145);


        /*----------------------------------------------------------------------------------------------------------------------*/
        //文本
        TextField textField =new TextField("测试文本");
        textField.setLayoutX(100);
        textField.setLayoutY(100);
//        textField.setPrefHeight(50);
//        textField.setPrefWidth(200);
        textField.setFont(Font.font(15));
        //textField.setStyle();

        // 设置提示
        Tooltip tip = new Tooltip("这是提示:");
        tip.setFont(Font.font(40));
        textField.setTooltip(tip);

        textField.setPromptText("请输入7个字以下:");
        textField.setFocusTraversable(false);
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

                if (newValue.length()> 7){
                    textField.setText(oldValue);
                }
                // System.out.println(newValue);
            }
        });

        textField.selectedTextProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println(newValue);
            }
        });

        /*----------------------------------------------------------------------------------------------------------------------*/
        // 密码

        PasswordField passwordField = new PasswordField();


        /*----------------------------------------------------------------------------------------------------------------------*/

        // 标签
        Label label = new Label("标签");
        label.setLayoutX(260);
        label.setLayoutY(260);


        label.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getButton().name().equals(MouseButton.PRIMARY.name())){
                    System.out.println("aaaaaaaa");
                }

            }
        });

//        label.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                //System.out.println("b1点击事件");
//                System.out.println("鼠标按键"+event.getButton().name());
//
//                if(event.getClickCount() == 2  && event.getButton().name().equals(MouseButton.PRIMARY.name())){
//
//                    System.out.println("双击事件");
//                }
//
//            }
//        });


//        label.setOnKeyPressed(new EventHandler<KeyEvent>() {
//            @Override
//            public void handle(KeyEvent event) {
//                System.out.println("按下"+event.getCode().getName());
//                System.out.println("按下");
//
//                if (event.getCode().getName().equals(KeyCode.A.getName())){
//                    System.out.println("按下= "+event.getCode().getName());
//                }
//            }
//        });


        /*----------------------------------------------------------------------------------------------------------------------*/
        Group root = new Group();
        root.getChildren().add(textField);
        root.getChildren().add(passwordField);
        root.getChildren().add(label);
        /*----------------------------------------------------------------------------------------------------------------------*/
        Scene scene = new Scene(root);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

AnchorPane布局类

 

package lesson13;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import jdk.management.resource.internal.ApproverGroup;


/**
 * @author major_s
 * @date 2020/6/19 - 9:18
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane ap = new AnchorPane();
        Button b1 = new Button("button1");
//        b1.setLayoutX(100);
//        b1.setLayoutY(100);
        Button b2 = new Button("buttom2");

        Button b3 =new Button("button3");

        Button b4 = new Button("button4");

//        ap.setPadding(new Insets(10));
//
//        ap.setTopAnchor(b1,10.0);
//        ap.setLeftAnchor(b1,10.0);
//        ap.setRightAnchor(b1,10.0);
//        ap.setBottomAnchor(b1,10.0);

        Group group1 = new Group();
        Group group2 = new Group();
        group1.getChildren().addAll(b1,b2);
        group2.getChildren().addAll(b3,b4);
        ap.setTopAnchor(group1,100.0);
        ap.setLeftAnchor(group1,100.0);


        AnchorPane ap2 = new AnchorPane();
        ap2.setStyle("-fx-background-color: #9BCD9B");
        ap2.getChildren().add(b1);
        ap2.setRightAnchor(b1,0.0);
        
        // b1.setManaged(false);  //脱离管理 
        // b1.setOpcity(1)  
        // b1.setVisiable(true)






//        b1.setLayoutX(100);
//        b1.setLayoutY(100);

        ap.getChildren().addAll(ap2);


        ap.setStyle("-fx-background-color: #FF3E96");
        ap.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {

                System.out.println("hello");
            }
        });



        Group root = new Group();
        //root.setStyle();
        //root.setOnMouseClicked();
        Scene scene = new Scene(ap);
        primaryStage.setScene(scene);
        primaryStage.setTitle("慧明");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();

        ap.setTopAnchor(ap2,10.0);
        ap.setLeftAnchor(ap2,10.0);
        ap.setRightAnchor(ap2,ap.getWidth()/2);
        ap.setBottomAnchor(ap2,ap.getHeight()/2);

        primaryStage.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

                ap.setBottomAnchor(ap2,ap.getHeight()/2);
            }
        });

        primaryStage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                ap.setRightAnchor(ap2,ap.getWidth()/2);
            }
        });
    }
}

HBox和VBox

 

package lesson14;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/19 - 11:03
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");


        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: #AEEEEE");

        //HBox box = new HBox();
        VBox box = new VBox();


        box.getChildren().addAll(b1,b2,b3);
        // 设置上下左右边距
        box.setPadding(new Insets(10));
        // 设置内空间
        box.setSpacing(10);
        
        // 设置单个组件边距
        box.setMargin(b1,new Insets(10));

        box.setPrefHeight(400);
        box.setPrefWidth(400);
        box.setStyle("-fx-background-color:#9BCD9B ");

        ap.getChildren().add(box);



//        AnchorPane ap2= new AnchorPane();
//        ap2.setStyle("-fx-background-color: #9BCD9B");
//        ap2.setPrefWidth(100);
//        ap2.setPrefHeight(100);

//        ap.getChildren().add(ap2);

//        ap.setTopAnchor(ap2,0.0);
//        ap.setLeftAnchor(ap2,0.0);

        Scene scene =new Scene(ap);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();




    }
}

setManaged,setVisible,setOpacity

 

package lesson15;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import javafx.scene.control.Button;

/**
 * @author major_s
 * @date 2020/6/20 - 12:59
 **/
public class Main extends Application {

    static boolean isManaged = false;
    static boolean isVisible = false;
    static int opacityValue = 0;

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("Button1");
        Button b2 = new Button("Button2");
        Button b3 = new Button("Button3");
        Button b4 = new Button("Button4");

        Button b5 = new Button("b3.setManaged(false);");
        Button b6 = new Button("b3.setVisible(false)");
        Button b7 = new Button("b3.setOpacity(0)");

//        b3.setManaged(false);
//        b3.setVisible(false);
//        b3.setOpacity(0);




        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: #AEEEEE");
        HBox hbox = new HBox();
        hbox.setPadding(new Insets(20));
        hbox.setSpacing(10);
        hbox.getChildren().addAll(b1,b2,b3,b4);

        VBox vbox = new VBox();
        //vbox.setPadding(new Insets(20));
        vbox.setSpacing(10);
        vbox.getChildren().addAll(b5,b6,b7);
        ap.setTopAnchor(vbox,100.0);
        ap.setLeftAnchor(vbox,20.0);


        ap.getChildren().addAll(hbox,vbox);




        Scene scene = new Scene(ap);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();

        b5.setOnAction(new EventHandler<ActionEvent>() {


            @Override
            public void handle(ActionEvent event) {
               b3.setManaged(isManaged);
               new Print(hbox);
               if(isManaged == true){
                   isManaged = false;
                   b5.setText("b3.setManaged("+isManaged+")");
               } else {
                   isManaged=true;
                   b5.setText("b3.setManaged("+isManaged+")");
               }

            }
        });

        b6.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                b3.setVisible(isVisible);
                new Print(hbox);
                if(isVisible == true){
                    isVisible = false;
                    b6.setText("b3.setVisible("+isVisible+")");
                } else {
                    isVisible=true;
                    b6.setText("b3.setVisible("+isVisible+")");
                }


            }
        });

        b7.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {


                b3.setOpacity(opacityValue);
                new Print(hbox);
                if(opacityValue == 0){
                    opacityValue = 1;
                    b7.setText("b3.setOpacity("+opacityValue+")");
                } else {
                    opacityValue=0;
                    b7.setText("b3.setOpacity("+opacityValue+")");
                }

            }
        });



    }
}

class Print{
    Print(HBox hbox){
        System.out.println("当前HBox里子组件数量="+hbox.getChildren().size());
    }
}

16.BorderPane布局类

 

package lesson16;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;



/**
 * @author major_s
 * @date 2020/6/20 - 13:55
 **/
public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b = new Button("button");

        AnchorPane a1 = new AnchorPane();
        a1.setPrefHeight(100);
        a1.setPrefWidth(100);
        a1.setStyle("-fx-background-color: #EE6AA7");

        AnchorPane a2 = new AnchorPane();
        a2.setPrefHeight(100);
        a2.setPrefWidth(100);
        a2.setStyle("-fx-background-color: #98FB98");

        AnchorPane a3 = new AnchorPane();
        a3.setPrefHeight(100);
        a3.setPrefWidth(100);
        a3.setStyle("-fx-background-color: #A0522D");

        AnchorPane a4 = new AnchorPane();
        a4.setPrefHeight(100);
        a4.setPrefWidth(100);
        a4.setStyle("-fx-background-color: #1E90FF");

        AnchorPane a5 = new AnchorPane();
        a5.setPrefHeight(100);
        a5.setPrefWidth(100);
        a5.setStyle("-fx-background-color: #EEEE00");

        BorderPane bor = new BorderPane();
        bor.setStyle("-fx-background-color: #B23AEE");

        bor.setTop(a1);
        bor.setBottom(a2);
        bor.setLeft(a3);
        bor.setRight(a4);
        bor.setCenter(a5);
//        // 设置内边距
//        bor.setPadding(new Insets(10));
//        // 设置 a1 外边距
//        bor.setMargin(a1,new Insets(10));
//        // 设置 b 对齐方式 居中
//        bor.setAlignment(b, Pos.CENTER);
//
//        Button bu =(Button)bor.getTop();
//        System.out.println(bu.getText());




        Scene scene = new Scene(bor);
        primaryStage.setScene(scene);
        primaryStage.setTitle("慧明");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();


    }
}

FlowPane布局类

 

package lesson17;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/20 - 14:21
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        Button b4 = new Button("button4");
        Button b5 = new Button("button5");
        Button b6 = new Button("button6");
        Button b7 = new Button("button7");
        Button b8 = new Button("button8");


        FlowPane flow = new FlowPane();
        flow.setStyle("-fx-background-color: #EE6AA7");
        flow.setPadding(new Insets(10));
      //  flow.setMargin(b1,new Insets(10));
        flow.setAlignment(Pos.CENTER);
        // 设置水平间距
        flow.setHgap(10);
        // 设置垂直间距
        flow.setVgap(20);

        // 设置流式布局的方向
        flow.setOrientation(Orientation.VERTICAL);

        flow.getChildren().addAll(b1,b2,b3,b4,b5,b6,b7,b8);

        Scene scene = new Scene(flow);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();



    }
}

GridPane布局类

 

package lesson18;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/20 - 15:00
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        Button b4 = new Button("button4");
        Button b5 = new Button("button5");
        Button b6 = new Button("button6");
        Button b7 = new Button("button7");
        Button b8 = new Button("button8");

        GridPane grid = new GridPane();
        grid.setStyle("-fx-background-color: #EE6AA7");

        // 设置组件在GridPane里的第几列第几行

        grid.add(b1,0,0);
        grid.add(b2,1,0);
        grid.add(b3,2,0);
        grid.add(b4,3,0);
        grid.add(b5,0,1);
        grid.add(b6,1,1);
        grid.add(b7,2,1);
        grid.add(b8,3,1);

        // 设置网格布局中水平垂直间隔
       // grid.setHgap(10);
        //grid.setVgap(10);

        // 设置grid中 b1 的外边距
        //grid.setMargin(b1,new Insets(10));

        // 设置grid布局为居中
        //grid.setAlignment(Pos.CENTER);

        // 设置某node到某位置 方式一
        //grid.setConstraints(b1,1,1);
        //grid.setChilden().addAll(b1);

        // 设置某node到某位置 方式二
//        grid.setRowIndex(b1,0);
//        grid.setColumnIndex(b2,1);
//        grid.getChildren().add(b1);

        // 设置第一列间距
        grid.getColumnConstraints().add(new ColumnConstraints(100));
        // 设置第一行间距
        grid.getRowConstraints().add(new RowConstraints(50));


        Scene scene =new Scene(grid);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();



    }
}

StackPane布局类

 

package lesson19;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.function.Consumer;

/**
 * @author major_s
 * @date 2020/6/20 - 15:33
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        Button b4 = new Button("button4");
        Button b5 = new Button("button5");

        StackPane stack = new StackPane();
        stack.setStyle("-fx-background-color: #EE6AA7");

        stack.setPadding(new Insets(10));
        stack.setMargin(b1,new Insets(10));

        stack.setAlignment(Pos.BOTTOM_LEFT);


        stack.getChildren().addAll(b1,b2,b3,b4,b5);
//        stack.getChildren().forEach(new Consumer<Node>() {
//            @Override
//            public void accept(Node t) {
//                Button bu = (Button)t;
//                System.out.println(bu.getText());
//            }
//        });

        // 简写
        stack.getChildren().forEach((item)->{

            Button bu = (Button)item;
            System.out.println(bu.getText());

        });
        Scene scene =new Scene(stack);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();
    }
}

TextFlow布局类

 

package lesson20;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

/**
 * @author major_s
 * @date 2020/6/20 - 18:35
 **/
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        Text t1 = new Text("1111111\n");
        Text t2 = new Text("2222222");
        Text t3 = new Text("3333333");

        t1.setFont(Font.font(30));
        t1.setFill(Paint.valueOf("#FF82AB"));
        TextFlow textFlow = new TextFlow();
        textFlow.setStyle("-fx-background-color: #EECFA1");
        textFlow.getChildren().addAll(t1,t2,t3);



        Scene scene = new Scene(textFlow);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();


    }
}