• java基础:10.6 事件驱动


    当运行一个JAVA GUI程序时,程序和用户进行交互,并且事件驱动它的执行。这叫事件驱动编程。产生一个事件并且触发它的组件称为事件源对象,或者简单称为源对象或者源组件。例如,一个按钮是一个按钮单击动作事件的源对象。一个事件是一个事件类的实例。可以通过EventObject 类中的getSource()实例方法来确定一个事件的源对象

    Java 事件类的根类是java.util.EventObject。JavaFX 的事件类的根类javafx.event.Event

    Event 包括:ActionEvent / InputEvent / WindowEvent 

    如果一个组件可以触发一个事件,那么这个组件的任何子类都可以触发同样类型的事件。比如,每个JavaFX 形状、布局面板和组件都可以融发MouseEvent 和KeyEvent 事件,因为Node 是形状、布局面板和组件的超类。

    要成为一个动作事件的处理器,必须满足两个要求:
    1 ) 该对象必须是EventHandler <T extends Event> 接口的一个实例。接口定义了所有处理器的共同行为。<T extends Event> 表示T 是一个Event 子类型的泛型。
    2) EventHandler 对象handler 必须使用方法source.setOnAction(handler)和事件源对象注册。处理器对象必须通过源对象进行注册。注册方法依赖于事件类型。对ActionEvent 而言,方法是setOnAction。对一个鼠标按下事件来说,方法是setOnMousePressed。对于一个按键事件,方法是set0nKeyPressed。

    学习代码:点击按钮可以放大/缩小圆

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    
    public class Chapter15_2 extends Application {
    	private CirclePane circlePane = new CirclePane();
    	
    	@Override
    	public void start(Stage primaryStage) {   
    		HBox hbox = new HBox(10);
    		hbox.setSpacing(10);
    		hbox.setAlignment(Pos.CENTER);
    		Button btEnlarge = new Button("Enlarge");
    		Button btShrink = new Button("Shrink");
    		hbox.getChildren().add(btEnlarge);
    		hbox.getChildren().add(btShrink);
    		
    		//create and register the handler
    		btEnlarge.setOnAction(new EnlargeHandler());
    		btShrink.setOnAction(new ShrinkHandler());
    		
    		BorderPane borderpane = new BorderPane();
    		borderpane.setCenter(circlePane);
    		borderpane.setBottom(hbox);
    		BorderPane.setAlignment(hbox,Pos.CENTER);
    		
    		Scene scene = new Scene(borderpane,200,150);
    		primaryStage.setTitle("ControlCircle");
    		primaryStage.setScene(scene);
    		primaryStage.show();
    		
    		
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Application.launch(args);
    	}
    
    	class EnlargeHandler implements EventHandler <ActionEvent> {
    		@Override
    		public void handle(ActionEvent e) {
    			circlePane.enlarge();
    		}
    	}
    
    	class ShrinkHandler implements EventHandler <ActionEvent> {
    		@Override
    		public void handle(ActionEvent e) {
    			circlePane.shrink();
    		}
    	}
    }
    
    class CirclePane extends StackPane{
    	private Circle circle = new Circle(50);
    	
    	public CirclePane() {
    		getChildren().add(circle);
    	    circle.setFill(Color.WHITE);
    	    circle.setStroke(Color.BLACK);
    	}
    	
    	public void enlarge() {
    		circle.setRadius(circle.getRadius() + 2 );
    	}
    	
    	public void shrink() {
    		circle.setRadius(circle.getRadius() >2 ? circle.getRadius()-2 : circle.getRadius());
    	}    
    }
    
    
    
  • 相关阅读:
    Android 从零开始打造异步处理框架
    Android 如何有效的解决内存泄漏的问题
    Android 弱引用和软引用
    Python 环境搭建,开发工具,基本语法
    Android 急速发布项目到 JitPack
    GitHub 实现多人协同提交代码并且权限分组管理
    Android 6.0 权限管理最佳实践
    Android Json处理框架
    Android Gson的使用总结
    Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
  • 原文地址:https://www.cnblogs.com/l20902/p/10610911.html
Copyright © 2020-2023  润新知