我在这里有我的KeyHandler:

private void KeyHandler(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:

        if(evt.getKeyCode() == KeyEvent.VK_UP)
        {
            if(menuPosition == 1){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_3.png")));
                menuPosition = 3;
            }
            else if(menuPosition == 2){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_1.png")));
                menuPosition = 1;
            }
            else if(menuPosition == 3){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_2.png")));
                menuPosition = 2;
            }
        }
        if(evt.getKeyCode() == KeyEvent.VK_DOWN)
        {
            if(menuPosition == 1){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_2.png")));
                menuPosition = 2;
            }
            else if(menuPosition == 2){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_3.png")));
                menuPosition = 3;
            }
            else if(menuPosition == 3){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_1.png")));
                menuPosition = 1;
            }
        }
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            if(menuPosition == 1){
                menuPosition = 5;
                Container parent = MenuLabel.getParent();
                parent.removeAll();
                parent.add(NewGamePanel);
                parent.repaint();
                parent.revalidate();

            }
            else if(menuPosition == 2){
                menuPosition = 4;
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/readme.png")));
            }
            else if(menuPosition == 3){
                System.exit(0);
            }
            else if(menuPosition == 4){
                MenuLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/labyrinth grafika/menu/menu_volba_1.png")));
                menuPosition = 1;
            }
        }
    }


而且我还有我的按钮:

private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:

        jPanel1.removeAll();
        jPanel1.add(MenuLabel);
        jPanel1.repaint();
        jPanel1.revalidate();
        menuPosition = 1;


    }


当JPanel1是我程序的面板时。我在那里有CardLayout,单击此按钮后,我想关闭所有内容,然后返回到我的MenuLabel。

在Java生成的initComponents中,有以下代码:

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    MenuLabel = new javax.swing.JLabel();
    NewGamePanel = new javax.swing.JPanel();
    NewGameHeader = new javax.swing.JLabel();
    NewGameBody = new javax.swing.JPanel();
    sizeOfField = new javax.swing.JComboBox();
    sizeOfFieldLabel = new javax.swing.JLabel();
    NewGameOkButton = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    numberOfPlayersCombo = new javax.swing.JComboBox();
    backButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Labyrinth - The Game");
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setMinimumSize(new java.awt.Dimension(1000, 650));
    setPreferredSize(new java.awt.Dimension(650, 700));
    setResizable(false);
    setSize(new java.awt.Dimension(650, 700));
    addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            KeyHandler(evt);
        }
    });
    ...


有趣的是,单击按钮后,我的KeyHandler无法工作,但是当我将以下简单代码放入backButtonActionPerformed函数中时:

JOptionPane.showMessageDialog(null, null);


keyHandler再次正常工作。谁能告诉我为什么,如何使KeyHandler再次工作而不弹出消息对话框?

其余代码由Java生成(某些其他框等)。

谢谢。

最佳答案

在您的函数backButtonActionPerformed(java.awt.event.ActionEvent evt)中尝试添加一行

jPanel1.requestFocusInWindow();


menuPosition = 1;行之前,并尝试运行该程序。

关于java - showMessageDialog影响KeyHandler,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30071858/

10-10 03:03