提问者:小点点

为什么JDialog没有触发键侦听器的keyP的方法?


这是我的代码

 JToolBar customizeKeys = new JToolBar();
 customizeKeys.add(new ChangeKeyListen("left"));
 private class ChangeKeyListen extends AbstractAction{
    private JDialog myDialog;
    class KeyGetter extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            OtherPanel.this.map(
                        KeyEvent.getKeyText(e.getKeyCode()),
                                            keyAction);
            myDialog.setVisible(false);
            myDialog.removeKeyListener(getKeyListeners()[0]);
        }
    };
    public ChangeKeyListen(String name) {
        super(name);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       myDialog = new JOptionPane().createDialog("Press a key");
       myDialog.setVisible(true);
       myDialog.requestFocusInWindow();
       System.out.println(myDialog.getFocusableWindowState());
       myDialog.addKeyListener(new KeyGetter());
       System.out.println( myDialog.getKeyListeners());
     }
}

我在这里尝试做的是,当用户单击添加到JToolBar的JButton及其动作属性时,将提示用户使用我自己定制的对话框。然后用户可以按任何键关闭对话框。(它实际上只是不可见)。当我运行应用程序时,一切看起来都很好。JToolBar看起来是正确的,按钮看起来是正确的。当我单击按钮时,当对话框弹出时,正确的控制器行为发生了。(只是可见)然而,当我按下一个键时,根本没有触发键适配器的键按方法。

我所做的调试工作是首先确保JDialog可以首先是可聚焦的,这样它就可以从键盘接收关键事件。我用这行

System.out.println(myDialog.getFocusableWindowState());

我在控制台上得到的是真的。接下来,我确保键侦听器已经设置好。我用

 System.out.println( myDialog.getKeyListeners());

这个打印出来

[Ljava.awt.event.KeyListener;@350b914b

我假设它是从堆中分配的对象的正确内存地址。

然后我检查了类似的线程。

我的问题不能是Jbutton监听器没有被触发,为什么?因为对话框出现了,我确保键监听器被添加了打印键监听器行。我不能使用用户在尝试使用键监听器中所说的,因为我需要监听按键,并在我的程序中稍后使用该按键。这也没有帮助为什么这个KeyEvent不起作用?因为我需要对按键的一般反应来获取按下了哪个键。

我知道keyP的没有被执行,因为我在方法和这个print语句中放了一个断点

   System.out.println(KeyEvent.getKeyText(e.getKeyCode()));  

没有在控制台上打印任何东西。

有人知道我如何解决这个问题吗?


共2个答案

匿名用户

您正在将KeyListener添加到JOptionPane创建的对话框中。

但是,焦点在对话框上的JButton上。KeyEvents仅分派到具有焦点的组件,因此永远不会调用您的键侦听器代码。

为什么要监听关闭对话框的任何键?这不是用户友好的。用户不知道这是关闭对话框的方法,因为这不是标准的UI约定。用户应该单击按钮关闭对话框。

如果您确实需要在对话框打开时侦听任何按键,请查看全局事件侦听器,其中显示了如何使用AWTEventListener侦听任何按键事件,而不管哪个组件具有焦点。

匿名用户

在处理了非常相似的问题后,我鼓励想要将侦听器添加到JDialog组件的人遵循此处显示的方法。它允许对组件进行更多控制。

以下示例演示了一个自定义对话框,该对话框在每次使用KeyListener更改文本时验证用户的输入。

public class DialogWithListener extends JDialog {
    private JTextField textField = new JTextField();
    private boolean userPressedOk = false;

    /**
     * Creates a dialog that lets the user enter text in a text field.
     * <p>
     * Each time the user presses a key, the text is validated using the
     * {@link Predicate}s in {@code predsAndMsgs}. If the text doesn't satisfy
     * all predicates, the dialog shows the message associated with the first
     * unsatisfied predicate.
     * 
     * @param predsAndMsgs
     *            a map from {@link Predicate}s to the messages we'll show to
     *            users if the text they entered doesn't satisfy the predicates
     */
    public DialogWithListener(Map<Predicate<String>, String> predsAndMsgs) {

        JLabel textFieldLabel = new JLabel("Enter text:");

        // Show this if the text the user entered satisfies our predicates
        String okText = "All good";

        JLabel statusLabel = new JLabel(okText);

        Object[] paneContent = { textFieldLabel, textField, statusLabel };

        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> {
            userPressedOk = true;
            setVisible(false);
        });

        Object[] options = { okButton };
        JOptionPane optionPane = new JOptionPane(paneContent,
                JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null,
                options);

        getContentPane().add(optionPane);
        setLocationRelativeTo(optionPane.getParent());

        setFocusTo(textField);

        // Check the user input each time a key is released
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent event) {
                validate(predsAndMsgs, textField.getText(), okText,
                        statusLabel, okButton);
            }
        });

        setModal(true);
        setResizable(false);

        pack();
    }

    /**
     * Validates the {@code textToValidate}.
     * <p>
     * The {@link Predicate}s in {@link predsAndMsgs} determine whether the text
     * is valid. If the text is invalid, we show the message that is associated
     * with the predicate and disable this dialog's OK button.
     * 
     * @param predsAndMsgs
     *            a map from {@link Predicate}s that must hold for the
     *            {@code textToValidate} to the messages we'll show to the user
     *            if a predicate is not satisfied.
     * @param textToValidate
     *            we validate this text against the {@link Predicate}s in
     *            {@link predsAndMsgs}
     * @param okText
     *            this text is shown if the {@code textToValidate} satisfies all
     *            predicates
     * @param statusLabel
     *            a {@link JLabel} that either shows the {@link okText} or the
     *            message of the first predicate that doesn't hold true for the
     *            {@link textToValidate}
     * @param okButton
     *            we enable and disable this button depending on whether the
     *            {@link textToValidate} is valid
     */
    private void validate(Map<Predicate<String>, String> predsAndMsgs,
            String textToValidate, String okText, JLabel statusLabel,
            JButton okButton) {
        // Get the first predicate that the text to validate doesn't satisfy
        Optional<Predicate<String>> unsatisfiedPredMaybe = predsAndMsgs
                .keySet().stream().filter(pred -> !pred.test(textToValidate))
                .findFirst();
        // At least one predicate was not satisfied
        if (unsatisfiedPredMaybe.isPresent()) {
            // Tell the user the text they entered can't be accepted
            String msg = predsAndMsgs.get(unsatisfiedPredMaybe.get());
            statusLabel.setText(msg);
            okButton.setEnabled(false);
        } else {
            statusLabel.setText(okText);
            okButton.setEnabled(true);
        }
        pack();
    }

    private void setFocusTo(JComponent comp) {
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent ce) {
                comp.requestFocusInWindow();
            }
        });
    }

    public Optional<String> display() {
        userPressedOk = false;
        // Because the dialog is modal it will block here
        setVisible(true);
        String dialogResult = null;
        if (userPressedOk) {
            dialogResult = textField.getText();
        }
        return Optional.ofNullable(dialogResult);
    }
}

这就是您创建和显示对话框的方式:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager
                        .getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException
                    | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame();
    JButton showDialogButton = new JButton("Show Dialog");

    // Define the predicates that the user entered-text should satisfy and
    // the messages shown to the user if it doesn't
    Map<Predicate<String>, String> predicatesAndMessages = new HashMap<>();
    Predicate<String> dontMentionHisName = text -> !text
            .contains("Voldemort");
    predicatesAndMessages.put(dontMentionHisName,
            "Sssh! You can't say that!");

    DialogWithListener dialog = new DialogWithListener(
            predicatesAndMessages);
    dialog.setTitle("My dialog");
    showDialogButton.addActionListener(e -> dialog.display().ifPresent(
            userText -> System.out.println(userText)));

    frame.getContentPane().add(showDialogButton);

    frame.pack();
    frame.setVisible(true);
}