2012-10-03 36 views
8

Tôi đang cố gắng thực hiện công việc JavaFX Mnemonic. Tôi có một số nút trên cảnh và những gì tôi muốn đạt được là để bắn sự kiện nút này bằng cách nhấn Ctrl + S. Dưới đây là một mã sc mã vạch:Sử dụng JavaFX 2.2 Chú thích (và máy gia tốc)

@FXML 
public Button btnFirst; 

btnFirst.getScene().addMnemonic(new Mnemonic(btnFirst, 
      new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN))); 

Ghi chú của nútParsing là sai. (Vâng, trong khi cố gắng để làm cho công việc này tôi đã cố gắng để đặt nó thành sự thật, nhưng không có kết quả). Tài liệu JavaFX tuyên bố rằng khi một Mnemonic được đăng ký trên một Scene, và KeyCombination đạt đến Scene không bị ảnh hưởng, thì Node đích sẽ được gửi một ActionEvent. Nhưng điều này không hoạt động, có thể, tôi đang làm sai ...

Tôi có thể sử dụng nút tiêu chuẩn (bằng cách đặt mnemonicParsing thành true và tiền tố 'F' letter bằng ký tự gạch dưới). Nhưng theo cách này, người dùng phải sử dụng phím Alt, mang đến một số hành vi lạ trên các trình duyệt có thanh menu (nếu ứng dụng được nhúng vào trang web hơn trình đơn của trình duyệt được kích hoạt sau khi kích hoạt sự kiện nút bằng cách nhấn Alt + S). Bên cạnh đó, cách tiêu chuẩn khiến không thể tạo các phím tắt như Ctrl + Shift + F3, v.v.

Vì vậy, nếu có cách nào đó để thực hiện công việc này?

Trả lời

20

Đối với trường hợp sử dụng của bạn, tôi nghĩ bạn thực sự muốn sử dụng máy gia tốc thay vì ghi nhớ.

button.getScene().getAccelerators().put(
    new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN), 
    new Runnable() { 
    @Override public void run() { 
     button.fire(); 
    } 
    } 
); 

Trong hầu hết các trường hợp, bạn nên sử dụng KeyCombination.SHORTCUT_DOWN làm công cụ sửa đổi, như trong đoạn mã trên. Một lời giải thích cho điều này là trong tài liệu KeyCombination:

Các sửa đổi phím tắt được sử dụng để đại diện cho một phím sửa đổi đó là sử dụng phổ biến trong các phím tắt trên nền tảng máy chủ. Điều này là dành cho kiểm soát mẫu trên Windows và meta (phím lệnh) trên Mac. Bằng cách sử dụng nhà phát triển công cụ sửa đổi phím tắt có thể tạo lối tắt độc lập nền tảng độc lập. Vì vậy, tổ hợp phím "Shortcut + C" được xử lý nội bộ dưới dạng "Ctrl + C" trên Windows và "Meta + C" trên Mac.

Nếu bạn muốn đặc biệt mã để chỉ xử lý một tổ hợp phím Ctrl + S tổ hợp phím, họ bạn có thể sử dụng:

new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN) 

Dưới đây là một ví dụ thực thi: sản lượng

import javafx.animation.*; 
import javafx.application.Application; 
import javafx.event.*; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.image.*; 
import javafx.scene.input.*; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class SaveMe extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
    final Label response = new Label(); 
    final ImageView imageView = new ImageView(
     new Image("http://icons.iconarchive.com/icons/gianni-polito/colobrush/128/software-emule-icon.png") 
    ); 
    final Button button = new Button("Save Me", imageView); 
    button.setStyle("-fx-base: burlywood;"); 
    button.setContentDisplay(ContentDisplay.TOP); 
    displayFlashMessageOnAction(button, response, "You have been saved!"); 

    layoutScene(button, response, stage); 
    stage.show(); 

    setSaveAccelerator(button); 
    } 

    // sets the save accelerator for a button to the Ctrl+S key combination. 
    private void setSaveAccelerator(final Button button) { 
    Scene scene = button.getScene(); 
    if (scene == null) { 
     throw new IllegalArgumentException("setSaveAccelerator must be called when a button is attached to a scene"); 
    } 

    scene.getAccelerators().put(
     new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN), 
     new Runnable() { 
     @Override public void run() { 
      fireButton(button); 
     } 
     } 
    ); 
    } 

    // fires a button from code, providing visual feedback that the button is firing. 
    private void fireButton(final Button button) { 
    button.arm(); 
    PauseTransition pt = new PauseTransition(Duration.millis(300)); 
    pt.setOnFinished(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent event) { 
     button.fire(); 
     button.disarm(); 
     } 
    }); 
    pt.play(); 
    } 

    // displays a temporary message in a label when a button is pressed, 
    // and gradually fades the label away after the message has been displayed. 
    private void displayFlashMessageOnAction(final Button button, final Label label, final String message) { 
    final FadeTransition ft = new FadeTransition(Duration.seconds(3), label); 
    ft.setInterpolator(Interpolator.EASE_BOTH); 
    ft.setFromValue(1); 
    ft.setToValue(0); 
    button.setOnAction(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent event) { 
     label.setText(message); 
     label.setStyle("-fx-text-fill: forestgreen;"); 
     ft.playFromStart(); 
     } 
    }); 
    } 

    private void layoutScene(final Button button, final Label response, final Stage stage) { 
    final VBox layout = new VBox(10); 
    layout.setPrefWidth(300); 
    layout.setAlignment(Pos.CENTER); 
    layout.getChildren().addAll(button, response); 
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;"); 
    stage.setScene(new Scene(layout)); 
    } 

    public static void main(String[] args) { launch(args); } 
} 
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/ 
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih 

mẫu:

Sample program output

+0

jewelsea, thank yo u. Bạn hoàn toàn đúng, gia tốc là trường hợp sử dụng của tôi. Tôi nhầm tưởng rằng các máy gia tốc phải được kết nối với các mục menu. Cảm ơn vì bài học của bạn. – bes67

+2

Lưu ý: thích "phím tắt" trên "điều khiển" (Windows) hoặc "meta" (Mac) để giữ cho nền tảng ứng dụng của bạn không độc lập. – Puce

+2

Cảm ơn Puce, bạn nên sử dụng 'SHORTCUT_DOWN' thay vì' CONTROL_DOWN'. Tôi đã cập nhật câu trả lời để đưa vào đề xuất này. – jewelsea

Các vấn đề liên quan