javafx.stage.Stage

public class Stage
extends Window
The JavaFX  Stage  class is the top level JavaFX container. The primary Stage is constructed by the platform . Additional Stage objects may be constructed by the application.

Stage objects must be constructed and modified on the JavaFX Application Thread.

Many of the Stage properties are read only because they can be changed externally by the underlying platform and therefore must not be bindable.

Style

A stage has one of the following styles:

The style must be initialized before the stage is made visible.

package stage;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class TestStage extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.initStyle(StageStyle.DECORATED);//StageStyle.DECORATED
        primaryStage.show();
    }
}
运行结果:

package stage;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class TestStage extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
//        primaryStage.initStyle(StageStyle.DECORATED);//StageStyle.DECORATED
        primaryStage.initStyle(StageStyle.UNDECORATED);//StageStyle.DECORATED
        primaryStage.show();
    }
}
运行结果:



package stage;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class TestStage extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
//        primaryStage.initStyle(StageStyle.DECORATED);//StageStyle.DECORATED
//        primaryStage.initStyle(StageStyle.UNDECORATED);//tageStyle.UNDECORATED
        primaryStage.initStyle(StageStyle.TRANSPARENT);//StageStyle.TRANSPARENT
        primaryStage.show();
    }
}
运行结果:


package stage;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class TestStage extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
//        primaryStage.initStyle(StageStyle.DECORATED);//StageStyle.DECORATED
//        primaryStage.initStyle(StageStyle.UNDECORATED);//tageStyle.UNDECORATED
//        primaryStage.initStyle(StageStyle.TRANSPARENT);//StageStyle.TRANSPARENT
        primaryStage.initStyle(StageStyle.UTILITY);//StageStyle.UTILITY
        primaryStage.show();
    }
}

运行结果:



Owner

A stage can optionally have an owner Window. When a window is a stage's owner, it is said to be the parent of that stage. When a parent window is closed, all its descendant windows are closed.The same chained behavior applied for a parent window that is iconified(最小化). A stage will always be on top of its parent window. The owner must be initialized before the stage is made visible.

Modality

A stage has one of the following modalities:

  • Modality.NONE - a stage that does not block any other window.
  • Modality.WINDOW_MODAL - a stage that blocks input events from being delivered to all windows from its owner (parent) to its root. Its root is the closest ancestor window without an owner.(将会阻止它的祖先们的输入事件)
  • Modality.APPLICATION_MODAL - a stage that blocks input events from being delivered to all windows from the same application, except for those from its child hierarchy.(除了自己,任何其他窗口都不能输入)

When a window is blocked by a modal stage its Z-order relative to its ancestors is preserved, and it receives no input events and no window activation(激活) events, but continues to animate and render normally. Note that showing a modal stage does not necessarily block the caller. The show() method returns immediately regardless of the modality of the stage. Use the showAndWait() method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.

 
import com.sun.prism.paint.Color;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
 
public class TestModality extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
               Stage stage = new Stage();
               VBox root = new VBox();
               TextField tx = new TextField();
               root.getChildren().addAll(tx);
               stage.setScene(new Scene(root,100,100));
               stage.initModality(Modality.WINDOW_MODAL);
               //去掉这一行,在Modality.WINDOW_MODAL下primaryStage是可以正常输入的,如果加上,就不能了
               stage.initOwner(primaryStage);//可以注释这一行来看看效果的不同,Modality.WINDOW_MODAL是阻止所有的父亲窗口的输入事件
//               stage.initModality(Modality.APPLICATION_MODAL);
               stage.show();
            }
        });
        TextField tx = new TextField();
        VBox root = new VBox();
        root.getChildren().addAll(btn,tx);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.initStyle(StageStyle.DECORATED);//StageStyle.DECORATED
        primaryStage.show();
    }
}

JDK简单的Example:

package stage;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class TestStage3 extends Application {

    @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group(new Text(25, 25, "Hello World!"))); 

        stage.setTitle("Welcome to JavaFX!"); 
        stage.setScene(scene); 
        stage.sizeToScene(); 
        stage.show(); 
    }

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


produces the following on Mac OSX:


produces the following on Windows XP:

produces the following on Windows Vista:


设置stage的背景的方法:

package stage;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TestStage2 extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        
        final Circle circ = new Circle(40, 40, 30);
        final Group root = new Group(circ);
        
        final Scene scene = new Scene(root, 800, 400, Color.BEIGE);

        final Text text1 = new Text(25, 25, "java2s.com");
        text1.setFill(Color.CHOCOLATE);
        text1.setFont(Font.font(java.awt.Font.SERIF, 25));
        root.getChildren().add(text1);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值