如何在javafx

时间:2015-07-08 11:10:35

标签: javafx javafx-2 javafx-8

如何在同一个字母中设置Mnemonic Parsing。在我的项目中设置Mnemonic in按钮,但按钮setText更改每个事件操作,但助记符在_o中相同但短键只能工作一个event.How如何解决这个问题

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author user
 */
public class JavaFXApplication4 extends Application {
    boolean b = false;
    @Override
    public void start(Stage primaryStage) {

    Button btn = new Button();
    btn.setText("Hell_o");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
           b = !b;
           if(!b){
              btn.setText("Hell_o");
               System.out.println("Hello");
           } else {
               btn.setText("w_orld");
               System.out.println("world");
           }
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    launch(args);
    }

}

对不起我的English

1 个答案:

答案 0 :(得分:1)

您需要在文本更改之前关闭助记符解析,并在更改之后再次打开它。方法setText()没有为助记符实现任何刷新,所以似乎所有都在场景中完成。

public class JavaFXApplication4 extends Application {

  boolean b = false;

  @Override
  public void start(Stage primaryStage) {

    Button btn = new Button();
    btn.setMnemonicParsing(true);
    btn.setText("Hell_o");
    btn.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        b = !b;
        btn.setMnemonicParsing(false);
        if (!b) {
          btn.setText("Hell_o");
          System.out.println("Hello");
        } else {
          btn.setText("W_orld");
          System.out.println("World");
        }
        btn.setMnemonicParsing(true);
      }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    launch(args);
  }
}