JavaFX 之弹跳球

[java]  view plain copy
  1. package FXExample;  
  2.   
  3. import javafx.animation.Animation;  
  4. import javafx.animation.KeyFrame;  
  5. import javafx.animation.Timeline;  
  6. import javafx.application.Application;  
  7. import javafx.application.Platform;  
  8. import javafx.event.ActionEvent;  
  9. import javafx.event.EventHandler;  
  10. import javafx.scene.Group;  
  11. import javafx.scene.Scene;  
  12. import javafx.scene.control.Menu;  
  13. import javafx.scene.control.MenuBar;  
  14. import javafx.scene.control.MenuItem;  
  15. import javafx.scene.control.RadioMenuItem;  
  16. import javafx.scene.control.RadioMenuItemBuilder;  
  17. import javafx.scene.control.SeparatorMenuItem;  
  18. import javafx.scene.control.ToggleGroup;  
  19. import javafx.scene.input.KeyCode;  
  20. import javafx.scene.input.KeyCodeCombination;  
  21. import javafx.scene.input.KeyCombination;  
  22. import javafx.scene.paint.Color;  
  23. import javafx.scene.paint.CycleMethod;  
  24. import javafx.scene.paint.RadialGradient;  
  25. import javafx.scene.paint.Stop;  
  26. import javafx.scene.shape.Circle;  
  27. import javafx.stage.Stage;  
  28. import javafx.util.Duration;  
  29.   
  30. public class DevelopingADialog extends Application{  
  31.   
  32.     static Stage LOGIN_DIALOG;  
  33.     static int dx = 1;  
  34.     static int dy = 1;  
  35.       
  36.     public static void main(String[] args) {  
  37.         launch(args);  
  38.   
  39.     }  
  40.   
  41.     private static Stage createLoginDialog(Stage parent, boolean modal){//modal:模式  
  42.         if(LOGIN_DIALOG != null){  
  43.             LOGIN_DIALOG.close();  
  44.         }  
  45.         return new MyDialog(parent,modal,"WelCome to javaFX");  
  46.           
  47.     }  
  48.     @Override  
  49.     public void start(final Stage primaryStage) throws Exception {  
  50.         primaryStage.setTitle("Chapter 1-17 Developing a Dialog");  
  51.         Group root = new Group();  
  52.         Scene scene = new Scene(root, 433312);  
  53.   
  54.         MenuBar menuBar = new MenuBar();  
  55.         menuBar.prefWidthProperty().bind(primaryStage.widthProperty());  
  56.           
  57.         Menu menu = new Menu("Home");  
  58.   
  59.         // add change password menu itme  
  60.         MenuItem newItem = new MenuItem("Change Password"null);  
  61.         newItem.setOnAction(new EventHandler<ActionEvent>(){  
  62.   
  63.             @Override  
  64.             public void handle(ActionEvent event) {  
  65.                 if(LOGIN_DIALOG == null){  
  66.                     LOGIN_DIALOG = createLoginDialog(primaryStage, true);  
  67.                 }  
  68.                 LOGIN_DIALOG.sizeToScene();  
  69.                 LOGIN_DIALOG.show();          
  70.             }  
  71.               
  72.         });  
  73.           
  74.         menu.getItems().add(newItem);  
  75.   
  76.         // add separator  
  77.         menu.getItems().add(new SeparatorMenuItem());  
  78.           
  79.      // add non modal menu item  
  80.         ToggleGroup modalGroup = new ToggleGroup();  
  81.         RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()  
  82.                 .toggleGroup(modalGroup)  
  83.                 .text("Non Modal")  
  84.                 .selected(true)  
  85.                 .build();  
  86.         nonModalItem.setOnAction(new EventHandler<ActionEvent>() {  
  87.   
  88.             public void handle(ActionEvent event) {  
  89.                 LOGIN_DIALOG = createLoginDialog(primaryStage, false);  
  90.             }  
  91.         });  
  92.   
  93.         menu.getItems().add(nonModalItem);  
  94.   
  95.         // add modal selection  
  96.         RadioMenuItem modalItem = RadioMenuItemBuilder.create()  
  97.                 .toggleGroup(modalGroup)  
  98.                 .text("Modal")  
  99.                 .selected(true)  
  100.                 .build();  
  101.         modalItem.setOnAction(new EventHandler<ActionEvent>() {  
  102.   
  103.             public void handle(ActionEvent event) {  
  104.                 LOGIN_DIALOG = createLoginDialog(primaryStage, true);  
  105.             }  
  106.         });  
  107.         menu.getItems().add(modalItem);  
  108.   
  109.         // add separator  
  110.         menu.getItems().add(new SeparatorMenuItem());  
  111.   
  112.         // add exit  
  113.         MenuItem exitItem = new MenuItem("Exit"null);  
  114.         exitItem.setMnemonicParsing(true);  
  115.         exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X, KeyCombination.CONTROL_DOWN));  
  116.         exitItem.setOnAction(new EventHandler<ActionEvent>() {  
  117.             public void handle(ActionEvent event) {  
  118.                 Platform.exit();  
  119.             }  
  120.         });  
  121.         menu.getItems().add(exitItem);  
  122.           
  123.      // add menu  
  124.         menuBar.getMenus().add(menu);  
  125.           
  126.      // menu bar to window  
  127.         root.getChildren().add(menuBar);  
  128.   
  129.         primaryStage.setScene(scene);  
  130.         primaryStage.show();  
  131.           
  132.         addBouncyBall(scene);  
  133.     }  
  134.       
  135.     private void addBouncyBall(final Scene scene){  
  136.         final Circle ball = new Circle(100,100,20);  
  137.         RadialGradient gradient1 = new RadialGradient(  
  138.                 0,  
  139.                 .1,  
  140.                 100,  
  141.                 100,  
  142.                 20,  
  143.                 false,  
  144.                 CycleMethod.NO_CYCLE,  
  145.                 new Stop(0, Color.WHITE),  
  146.                 new Stop(1, Color.GREEN));  
  147.         ball.setFill(gradient1);  
  148.           
  149.         final Group root = (Group) scene.getRoot();  
  150.         root.getChildren().add(ball);  
  151.           
  152.         Timeline tl = new Timeline();  
  153.         tl.setCycleCount(Animation.INDEFINITE);//indefinite:不确定的。  
  154.           
  155.         KeyFrame moveBall = new KeyFrame(Duration.seconds(.01500),  
  156.                 new EventHandler<ActionEvent>(){  
  157.   
  158.                     @Override  
  159.                     public void handle(ActionEvent event) {  
  160.                         double xMin = ball.getBoundsInParent().getMinX();  
  161.                         double yMin = ball.getBoundsInParent().getMinY();  
  162.                         double xMax = ball.getBoundsInParent().getMaxX();  
  163.                         double yMax = ball.getBoundsInParent().getMaxY();  
  164.                           
  165.                         if(xMin<0 || xMax>scene.getWidth()){  
  166.                             dx = dx * -1;  
  167.                         }  
  168.                         if (yMin < 0 || yMax > scene.getHeight()) {  
  169.                             dy = dy * -1;  
  170.                         }  
  171.                           
  172.                         ball.setTranslateX(ball.getTranslateX() + dx);  
  173.                         ball.setTranslateY(ball.getTranslateY() + dy);  
  174.                           
  175.                     }  
  176.                 });  
  177.         tl.getKeyFrames().add(moveBall);  
  178.         tl.play();  
  179.     }  
  180.   
  181. }  
[java]  view plain copy
  1. package FXExample;  
  2.   
  3. import javafx.event.ActionEvent;  
  4. import javafx.event.EventHandler;  
  5. import javafx.geometry.HPos;  
  6. import javafx.geometry.Insets;  
  7. import javafx.scene.Group;  
  8. import javafx.scene.Scene;  
  9. import javafx.scene.control.Button;  
  10. import javafx.scene.control.Label;  
  11. import javafx.scene.control.PasswordField;  
  12. import javafx.scene.control.TextField;  
  13. import javafx.scene.layout.GridPane;  
  14. import javafx.stage.Modality;  
  15. import javafx.stage.Stage;  
  16.   
  17. public class MyDialog extends Stage {  
  18.     public MyDialog(Stage owner, boolean modality, String title){  
  19.         super();  
  20.         initOwner(owner);  
  21.         Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;  
  22.         initModality(m);  
  23.         setOpacity(.90);  
  24.         setTitle(title);  
  25.         Group root = new Group();  
  26.         Scene scene = new Scene(root, 250150);  
  27.         setScene(scene);  
  28.           
  29.         GridPane gridpane = new GridPane();  
  30.         gridpane.setPadding(new Insets(5));  
  31.         gridpane.setHgap(5);  
  32.         gridpane.setVgap(5);  
  33.   
  34.         Label mainLabel = new Label("Enter User Name & Password");  
  35.         gridpane.add(mainLabel, 1021);  
  36.   
  37.   
  38.         Label userNameLbl = new Label("User Name: ");  
  39.         gridpane.add(userNameLbl, 01);  
  40.   
  41.         Label passwordLbl = new Label("Password: ");  
  42.         gridpane.add(passwordLbl, 02);  
  43.   
  44.   
  45.         // username text field  
  46.         final TextField userNameFld = new TextField("Admin");  
  47.         gridpane.add(userNameFld, 11);  
  48.   
  49.         // password field  
  50.         final PasswordField passwordFld = new PasswordField();  
  51.         passwordFld.setText("drowssap");  
  52.         gridpane.add(passwordFld, 12);  
  53.           
  54.         Button login = new Button("Change");  
  55.         login.setOnAction(new EventHandler<ActionEvent>() {  
  56.   
  57.             public void handle(ActionEvent event) {  
  58.                 close();  
  59.             }  
  60.         });  
  61.         gridpane.add(login, 13);  
  62.         GridPane.setHalignment(login, HPos.RIGHT);  
  63.         root.getChildren().add(gridpane);  
  64.     }  
  65. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值