【问题标题】:JavaFX program toggles full-screen when enter key is pressed inside of a TextField当在 TextField 内按下回车键时,JavaFX 程序切换全屏
【发布时间】:2018-12-16 11:55:10
【问题描述】:

每当同时按下两个键盘键 alt 和 enter 时,我都会让我的程序进入全屏状态。这主要按预期工作。

问题是我的程序会在按下回车键时切换全屏模式。是否按下 alt 键无关紧要。

我怎样才能使程序在仅按下回车键时不会切换全屏模式。

我正在使用 OpenJFX 11。

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;


public class Main extends Application {
   final KeyCombination FullScreenKeyCombo = 
         new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_ANY);

    @Override
    public void start(Stage stage) {
            GridPane grid = new GridPane();
            Scene scene = new Scene(grid, 1600, 900);
            stage.setScene(scene);
            stage.show();

         // create TextField and add to GridPane
         TextField textField = new TextField();
         grid.add(textField, 0, 0);

         // toggle full-screen when alt + enter is pressed
         scene.addEventHandler(KeyEvent.KEY_PRESSED, event -> {

            if(FullScreenKeyCombo.match(event)) {

               stage.setFullScreen(!stage.isFullScreen());

            }
         });

    }

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

【问题讨论】:

  • 不是答案,只是一点点:整个内部if 可以替换为stage.setFullScreen(!stage.isFullScreen());
  • 谢谢,我会编辑我的帖子来添加这个。
  • ALT_ANY 的意思是“我不在乎 Alt 键是否被按下。”你想要ALT_DOWN
  • @VGR 这解决了我的问题!如果您想发布答案,我会接受。
  • ^^ Indeed;请把它作为一个答案,以便它可以被接受。

标签: java javafx fullscreen textfield


【解决方案1】:

在这一行:

new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_ANY);

ALT_ANY 的意思是“我不在乎 Alt 键是否被按下。”

改用ALT_DOWN

new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_DOWN);

【讨论】:

  • 再次感谢。我应该检查一下,但我认为这没有意义,因为我不知道为什么有人必须特别说:“我不在乎是否按下了 X 键"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
相关资源
最近更新 更多