JavaFX 设置快捷键

package fx.com;

import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;
import sun.net.www.content.image.png;


import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main extends Application {
    public static void main(String[] args) {
        launch(Main.class, args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException, InterruptedException {

        Button button1 = new Button("这是按钮");

        button1.setLayoutX(10);
        button1.setLayoutY(10);
        button1.setPrefWidth(200);
        button1.setPrefHeight(100);
        button1.setFont(Font.font(20));


        Group group = new Group();
        group.getChildren().add(button1);

        Scene scene = new Scene(group);

        //第一种 win10可能有问题
        KeyCombination kc1 = new KeyCodeCombination(KeyCode.C, KeyCombination.ALT_DOWN, KeyCombination.CONTROL_DOWN);
        Mnemonic mnemonic1 = new Mnemonic(button1, kc1);
        scene.addMnemonic(mnemonic1);
        //第二种 win10可能有问题
        KeyCombination kc2 = new KeyCharacterCombination("O", KeyCombination.ALT_DOWN);
        Mnemonic mnemonic2 = new Mnemonic(button1, kc2);
        scene.addMnemonic(mnemonic2);

        //第三种 SHORTCUT_DOWN = ctrl 苹果系统也支持
        KeyCombination kc3 = new KeyCodeCombination(KeyCode.Z,KeyCombination.SHORTCUT_DOWN);
        scene.getAccelerators().put(kc3, new Runnable() {
            @Override
            public void run() {
                System.out.println("方法");
                button1.fire();
            }
        });




        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("按钮的方法");
            }
        });

        primaryStage.setScene(scene);
        URL qunlogoUrl = new URL("http://www.haotuo.net.cn/Resources/cq/qunlogo.png");
        primaryStage.setWidth(800);
        primaryStage.setHeight(500);
        primaryStage.getIcons().add(new Image(qunlogoUrl.toExternalForm()));
        primaryStage.setTitle("JavaFX");
        primaryStage.show();


    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值