Lesson15 setManaged,setVisible,setOpacity

写在main方法前的static方法在执行的时候会先于main()执行.
这节课为以前的内容小结练习案例,示例如下

static boolean isManaged = false;
static boolean isVisible = false;
static double Opacityvalue = 0.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("button5");
    Button b6 = new Button("button6");
    Button b7 = new Button("button7");

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(10));
    hbox.setSpacing(10);
    hbox.getChildren().addAll(b1,b2,b3,b4);
    VBox vbox = new VBox();
    vbox.setPadding(new Insets(10));
    vbox.setSpacing(10);


    vbox.getChildren().addAll(b5,b6,b7);
    AnchorPane ap = new AnchorPane();
    ap.getChildren().addAll(hbox,vbox);
    ap.setTopAnchor(vbox,100.0);//设置vbox的外边距
    ap.setLeftAnchor(vbox,0.0);//设置vbox的外边距
    Scene scene = new Scene(ap);

    primaryStage.setScene(scene);
    primaryStage.setTitle("test1");
    primaryStage.setWidth(800);
    primaryStage.setHeight(800);
    primaryStage.show();

    b5.setOnAction(new EventHandler() {
        @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() {
        @Override
        public void handle(ActionEvent event) {
            b3.setVisible(isVisible);
            new Print(hbox);
            if (isVisible == true){
                isVisible = false;
                b6.setText("b3.setManaged("+isVisible+");");
            }else {
                isVisible = true;
                b6.setText("b3.setManaged("+isVisible+");");
            }
        }
    });
    b7.setOnAction(new EventHandler() {
        @Override
        public void handle(ActionEvent event) {
            b3.setOpacity(Opacityvalue);
            new Print(hbox);
            if (Opacityvalue == 0.0){
                Opacityvalue = 1.0;
                b7.setText("b3.setManaged("+Opacityvalue+");");
            }else {
                Opacityvalue = 0.0;
                b7.setText("b3.setManaged("+Opacityvalue+");");
            }
        }
    });
}
class Print{
    Print(HBox hbox){
        System.out.println("当前HBox里子组件数量=" + hbox.getChildren().size());
    }
}

你可能感兴趣的:(Lesson15 setManaged,setVisible,setOpacity)