zoukankan      html  css  js  c++  java
  • JavaFx EventHandler

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.MenuItem;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            Application.launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("Title");
            Group root = new Group();
            Scene scene = new Scene(root, 400, 250, Color.WHITE);
    
            MenuBar menuBar = new MenuBar();
            EventHandler<ActionEvent> action = changeTabPlacement();
    
            Menu menu = new Menu("Direction");
            MenuItem left = new MenuItem("Left");
    
            left.setOnAction(action);
            menu.getItems().add(left);
    
            MenuItem right = new MenuItem("Right");
            right.setOnAction(action);
            menu.getItems().add(right);
    
            MenuItem top = new MenuItem("Top");
            top.setOnAction(action);
            menu.getItems().add(top);
    
            MenuItem bottom = new MenuItem("Bottom");
            bottom.setOnAction(action);
            menu.getItems().add(bottom);
    
            menuBar.getMenus().add(menu);
    
            BorderPane borderPane = new BorderPane();
    
            borderPane.prefHeightProperty().bind(scene.heightProperty());
            borderPane.prefWidthProperty().bind(scene.widthProperty());
            
            borderPane.setTop(menuBar);
            
            root.getChildren().add(borderPane);
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        private EventHandler<ActionEvent> changeTabPlacement() {
            return new EventHandler<ActionEvent>() {
    
                public void handle(ActionEvent event) {
                    MenuItem mItem = (MenuItem) event.getSource();
                    String side = mItem.getText();
                    if ("left".equalsIgnoreCase(side)) {
                        System.out.println("left");
                    } else if ("right".equalsIgnoreCase(side)) {
                        System.out.println("right");
                    } else if ("top".equalsIgnoreCase(side)) {
                        System.out.println("top");
                    } else if ("bottom".equalsIgnoreCase(side)) {
                        System.out.println("bottom");
                    }
                }
            };
        }
    }
  • 相关阅读:
    linux 查看磁盘空间大小
    Redis内存碎片率
    redis的incr和incrby命令
    redis如何清空当前缓存和所有缓存
    ArcGIS矢量数据批量合并工具
    arcgis 获得工具有多少个
    GoogleEarth二次开发难点和技巧
    ArcGIS 智能批量赋高程工具
    arcgis python支持汉字
    ArcGIS 宗地图批量打印输出
  • 原文地址:https://www.cnblogs.com/rojas/p/4720258.html
Copyright © 2011-2022 走看看