效果图
在按动按键时,圆圈可以调整大小。
单击鼠标左键,圆圈变大;单击鼠标右键,圆圈变小。
按键盘“U”键,圆圈变大;按键盘“D”键,圆圈变小。
关键步
Button btShrink = new Button("Shrink");
btShrink.setOnAction(e -> {
circlePane.shrink();
}
});
完整代码实现
package sample;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class Main extends Application {
private CirclePane circlePane = new CirclePane();
@Override
public void start(Stage primaryStage) throws Exception{
HBox hBox = new HBox();
hBox.setSpacing(10); //HBox中每个孩子之间的水平距离
hBox.setAlignment(Pos.CENTER); //所有孩子整体在hBox中的位置居中
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
hBox.getChildren().addAll(btEnlarge, btShrink);
// 事件处理器对象 listener, 以下为注册的三种方式
// 1 在事件源对象注册 source.setOnXEventType(listener)
btEnlarge.setOnAction(new EnlargeHandler());
// 2 匿名内部类
btShrink.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
circlePane.shrink();
}
});
// 3 lambda
circlePane.setOnMouseClicked(e -> {
// 鼠标左键单击
if (e.getButton() == MouseButton.PRIMARY) {
circlePane.enlarge();
}
// 鼠标右键单击
else if (e.getButton() == MouseButton.SECONDARY) {
circlePane.shrink();
}
circlePane.requestFocus(); //刷新,否则不能接收到键盘事件了
});
circlePane.setOnKeyPressed(e -> {
// 按下“U”键
if (e.getCode() == KeyCode.U) {
circlePane.enlarge();
}
// 按下“D”键
else if (e.getCode() == KeyCode.D) {
circlePane.shrink();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
//BorderPane.setAlignment(hBox, Pos.CENTER); //设定hBox.Alignment, 重复
Scene scene = new Scene(borderPane,200,150);
primaryStage.setScene(scene);
primaryStage.setTitle("Handle Event");
primaryStage.show();
circlePane.requestFocus();//刷新
}
// 事件处理器:实现EventHandler <T extends Event>接口
class EnlargeHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
circlePane.enlarge();
}
}
public static void main(String[] args) {launch(args); }
}
class CirclePane extends StackPane {
private Circle circle = new Circle(50);
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void enlarge() {
circle.setRadius(circle.getRadius() + 2);
}
public void shrink() {
circle.setRadius(circle.getRadius() > 2 ?
circle.getRadius() - 2 : circle.getRadius());
}
}
事件触发编程
事件处理过程中的三方:
- 事件源对象,又称源对象、源组件。产生事件的一方。例如按钮。
- 事件对象。根类:
java.util.EventObject
,javafx.event.Event
。 - 事件处理对象。有两个条件:
(1)实现EventHandler <T extends Event>
接口。
(2)与事件源对象绑定。可以用 EventObject 类中的getSource()
方法确定事件的源对象。
事件注册方法总结
用户动作 源对象 事件类型 事件注册方法
单机按钮 Button ActionEvent setOnAction(EventHandler<ActionEvent>)
在文本域回车 TextField .. ..
(取消)勾选 RadioButton/CheckBox ..
选择新项 ComboBox .. ..
按下鼠标 Node/Scene MouseEvent setOnMousePressed(EventHandler<MouseEvent>)
释放鼠标 .. .. setOnMouseReleased(EventHandler<MouseEvent>)
单击鼠标 .. .. ..Clicked..
鼠标进入 .. .. ..Entered..
鼠标退出 .. .. ..Exited..
鼠标移动 .. .. ..Moved..
鼠标拖动 .. .. ..Dragged..
按下键 Node/Scene KeyEvent setOnKeyPressed(EventHandler<KeyEvent>)
释放键 .. .. ..Released..
敲击键 .. .. ..Typed..
键盘事件
方法返回 | getCode() | getText() | getCharacter() |
---|---|---|---|
按下键和释放键 | (enum)KeyCode常量 | 描述字符串 | 空字符串 |
敲击键 | UNDEFINED | – | Unicode字符或字符序列 |
来源:CSDN
作者:酸甜梅子
链接:https://blog.csdn.net/weixin_44637071/article/details/104301852