JavaFX 事件处理

基本概念
事件(event)就是用户使用鼠标或键盘对窗口中的控件进行交互时发生的事情
所谓事件源(event source)就是能够产生事件并触发它的控件
事件源和监听者之间是多对多的关系

利用匿名内部类充当监听

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane borderPane = new BorderPane();
        Button button = new Button("color");
        TextArea textArea = new TextArea("FONT");
        borderPane.setBottom(button);
        borderPane.setTop(textArea);
        primaryStage.setScene(new Scene(borderPane,500,500));
        primaryStage.show();
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                textArea.setStyle("-fx-text-fill:red");
            }
        });

    }

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

利用Lambda表达式充当监听者

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;



public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane borderPane = new BorderPane();
        Button button = new Button("color");
        TextArea textArea = new TextArea("FONT");
        borderPane.setBottom(button);
        borderPane.setTop(textArea);
        primaryStage.setScene(new Scene(borderPane,500,500));
        primaryStage.show();
        button.setOnAction(actionEvent -> textArea.setStyle("-fx-text-fill:red"));

    }


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

JavaFX 主要事件类继承
在这里插入图片描述

拖拽功能

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;


public class Main extends Application {

    Text text = new Text(20,20,"Drag");
    private double tOFFX,tOFFY;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Pane pane = new Pane();
        pane.getChildren().add(text);
        Scene scene = new Scene(pane,300,300);
        text.setOnMousePressed(this::handleMousePress);
        text.setOnMouseDragged(this::handleMouseDragged);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleMouseDragged(MouseEvent e) {
        text.setX(e.getSceneX()-tOFFX);
        text.setY(e.getSceneY()-tOFFY);
    }


    protected void handleMousePress(MouseEvent e){
        tOFFX = e.getSceneX()-text.getX();
        tOFFY = e.getSceneY()-text.getY();
    }


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

KeyEvent
在这里插入图片描述
复选框和单选框及相应的事件处理
要判断一个复选框是否被选中,可以调用isSelected()方法;
单选框同理

文本编辑控件及相应的事件处理
当焦点位于其中时,按Enter 键发动作事件ActionEvent

组合框及相应的事件处理
组合框有两种模式:
1.默认状态下的不可编辑模式,这种模式下用户只能在下拉列表提供的一项中选择一项
2.另一种是可编辑模式,其特点是可以在显示栏中输入组合框列表中不包括的内容。
private ComboBox<String> cbo = new ComboBox<String>();

为绑定属性添加监听者
所有跟属性变化相关的事件,都可以用XXX.xxxProperty().addListener的形式来监听

列表视图控件及相应的事件处理
列表视图控件javafx.scene.control.ListView的功能与组合框相似,列表视图中显示出多个选项供用户选择,而且也可设置是否可以进行多项选择,还可设置列表视图是否可以编辑。
为了使列表视图具有滚动功能,可以将列表视图添加到一个滚动面板ScrollPane中,这样当列表视图中的内容超出可视区域时,滚动条就会自动出现。JavaFX是用带有类型参数的泛型类ListView来创建列表视图控件,类型参数T为存储在列表视图中的元素指定了数据类型。

private ListView<String> lv = new ListView <String> (items);
lv.getSelectiongModel().selectedItemProperty().addListener(new IListener());

滑动条及事件的处理
由于滚动条的valueProperty是一个绑定属性,所以可以为valueProperty属性添加监听者

package sample;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;


public class Main extends Application {

    private final Slider sl = new Slider(0.0,100.0,0.5);
    private final Text text = new Text("Text");

    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane borderPane = new BorderPane();
        borderPane.setRight(sl);
        borderPane.setCenter(text);
        sl.setShowTickLabels(true);
        sl.setValue(text.getFont().getSize());
        sl.setOrientation(Orientation.VERTICAL);
        sl.valueProperty().addListener(ov->
        {
            double size = sl.getValue();
            Font text1 = new Font(size);
            text.setFont(text1);

        });
        primaryStage.setScene(new Scene(borderPane,300,300));
        primaryStage.show();
    }



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

进度条及相应事件处理

菜单设计
菜单通常分为两种:一种是窗口菜单或称下拉式菜单;
另一种是上下文菜单,也称弹出菜单。

创建一个菜单系统,首先创建一个菜单栏对象MenuBar,再在菜单栏上添加若干个菜单对象Menu,在每一个菜单栏对象上再添加若干个菜单项,菜单项是MenuItem、CheckMenuItem、RadioMenuItem的对象。

MenuBar
向菜单栏上添加菜单,实际上是将菜单添加到ObservableList上。可以用过调用getMneus()的方法返回由菜单栏管理的菜单列表ObservableList的对象,然后调用add()或者addAll()方法,将Menu对象添加到该菜单列表。

Menu
与MenuBar添加节点的方式同理
可以通过remove()方式从菜单中删除不需要的菜单选项
菜单栏分割线 SeparatorMenuItem

MenuItem
MenuItem对象被选中时会产生动作事件,可以调用setOnAction()方法MenuItem对象注册监听者。

Menu和MenuItem设置热荐,只需在菜单和菜单项中为想要用作热键的字符前面添加一条下划线即可。
例如:将菜单栏显示名字File中的字符F设置为菜单中fileMenu的热键
Menu fileMenu = new Menu("_File");

若要为菜单单项设置快捷键,需要调用MenuItem的setAccekerator(KeyCombination value)方法
openMI.setAccelerator(KeyCombination.KeyCombination("Ctrl+O");

复选菜单栏CheckMenuItem和菜单菜单项RadioMenuItem

窗口菜单
Menu既可以作为顶层菜单添加到菜单栏,又可以作为子菜单添加到其他菜单栏。
如果一个菜单加入到另一个菜单中,则构成二级子菜单。

package sample;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;


public class Main extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane borderPane = new BorderPane();
        MenuBar menuBar = new MenuBar();
        borderPane.setTop(menuBar);
        Menu menu = new Menu("文件");
        Menu menu1 = new Menu("新建");
        menuBar.getMenus().addAll(menu,menu1);
        Menu menu2 = new Menu("打开");
        Menu menu3 = new Menu("退出");
        menu.getItems().addAll(menu3,menu2);
        CheckMenuItem checkMenuItem1 = new CheckMenuItem("Yes");
        CheckMenuItem checkMenuItem2 = new CheckMenuItem("No");
        menu3.getItems().addAll(checkMenuItem1,checkMenuItem2);
        primaryStage.setScene(new Scene(borderPane,300,300));
        primaryStage.show();

    }

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

弹出菜单
当某个控件上右击时,会弹出一个菜单供选择
ContextMenu();

工具栏设计
虽然可以把任意控件添加到工具栏中,但工具栏上的控件通常是以图标形式出现的,由于图表不是控件,所以他们不能直接放到工具栏上。
可以在工具栏上放置命令按钮,再把图标设置在命令按钮上。

文件选择对话框
比较重要,但内容较多

颜色选择器
ColorPicker

音频与视频程序设计
Media()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值