java 我能阻止JDialog在按下escape/enter键时关闭吗?

esbemjvw  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(200)

我创建了一个对话框来接收用户的击键来更改菜单项的键绑定。我希望enter和escape成为用户可以绑定的键,但是它们都关闭了对话框。如何拦截这些按键呢?
编辑:使用JOptionPane和自定义组件创建对话框

GetKeyComponent comp = new GetKeyComponent(accels, menuItem);
Object[] array = { comp };
JOptionPane optionPane = new JOptionPane(array, 
    JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
htrmnn0y

htrmnn0y1#

编辑:使用JOptionPane和自定义组件创建对话框
一个解决方案:不要这样做。创建您自己的模态JDialog,设置它的KeyBindings,然后使用它。例如,

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;    
import javax.swing.*;

public class JDialogTest {
   private static void createAndShowGUI() {
      final JFrame frame = new JFrame("JDialogTest");

      JPanel panel = new JPanel();
      panel.add(new JButton(new AbstractAction("Push Me") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            final JTextArea textArea = new JTextArea(15, 30);
            textArea.setFocusable(false);
            JDialog dialog = new JDialog(frame, "Dialog", true);

            int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
            JPanel contentPane = (JPanel) dialog.getContentPane();
            InputMap inputMap = contentPane.getInputMap(condition);
            ActionMap actionMap = contentPane.getActionMap();

            KeyStroke enterKs = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            inputMap.put(enterKs, enterKs.toString());
            actionMap.put(enterKs.toString(), new AbstractAction() {

               @Override
               public void actionPerformed(ActionEvent arg0) {
                  textArea.append("Enter pushed\n");
               }
            });

            KeyStroke escKs = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
            inputMap.put(escKs, escKs.toString());
            actionMap.put(escKs.toString(), new AbstractAction() {

               @Override
               public void actionPerformed(ActionEvent arg0) {
                  textArea.append("Escape pushed\n");
               }
            });

            dialog.add(new JScrollPane(textArea));
            dialog.pack();
            dialog.setVisible(true);
         }
      }));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}
axkjgtzd

axkjgtzd2#

作为单独类的@HovercraftFullOfEels代码(边距和按钮大小与标准Swing(又名“金属”)外观匹配):

public class SimpleDialog {
    static void showDialog(JFrame parent, JPanel message, String title) {
        JDialog dialog = new JDialog(parent, title, true);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        panel.add(message);

        panel.add(Box.createRigidArea(new Dimension(5, 10)));

        JButton closeButton = new JButton("OK");
        closeButton.setMargin(new Insets(2, 8, 2, 8));
        closeButton.addActionListener(e -> {
            dialog.dispose();
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        buttonPanel.add(closeButton, new GridBagConstraints());
        panel.add(buttonPanel);

        JPanel contentPane = (JPanel) dialog.getContentPane();
        InputMap inputMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = contentPane.getActionMap();
        KeyStroke escKs = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        inputMap.put(escKs, "Esc");
        actionMap.put("Esc", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dialog.dispose();
            }
        });

        dialog.add(panel);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        closeButton.requestFocus();
        dialog.setVisible(true);
    }
}

相关问题