KeyListener没有到达keyPressed方法

时间:2017-07-03 13:19:02

标签: java

目前正试图制作一个小游戏,我在移动我制作的立方体时遇到了问题,经过了调试,当我按下w,a,s或d时它没有移动到keyPressed方法,当我的应用程序打开我放置的按钮时,我也遇到了问题,直到我将鼠标悬停在它们上面,而且我的JTextField根本没有出现,帮助会非常感谢

public class littlegame extends JFrame implements ActionListener, WindowListener, KeyListener {

Color color_grey = new Color (188, 188, 188);

JButton btn_Quit, btn_Menu;

int charx = 200;
int chary = 200;

private Rectangle rect;

public static void main(String[] args) {
    littlegame littleGame = new littlegame();
    littleGame.runapplication();
}

private void runapplication() {

    setSize(1600,800);
    setLocation(50,50);
    setTitle("Little Game");
    setResizable(false);

    this.addKeyListener(this);

    addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });

    SpringLayout springLayout = new SpringLayout();
    setLayout(springLayout);

    JButtons(springLayout);
    JTextFields(springLayout);
    rect = new Rectangle(200,200,50,50);

    setVisible(true);

}

private void JButtons(SpringLayout layout)  {

    btn_Menu = new JButton ("Menu");
    this.add(btn_Menu);
    layout.putConstraint(SpringLayout.WEST, btn_Menu, 10, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.NORTH, btn_Menu, 15, SpringLayout.NORTH, this);
    btn_Menu.addActionListener(this);

    btn_Quit = new JButton ("Quit");
    this.add(btn_Quit);
    layout.putConstraint(SpringLayout.WEST, btn_Quit, 1525, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.NORTH, btn_Quit, 15, SpringLayout.NORTH, this);
    btn_Quit.addActionListener(this);
}

private void JButtonMethod(SpringLayout layout)    {


}

private void JTextFields(SpringLayout layout)   {

    JTextField txt_Menu_Background = new JTextField ();
    this.add(txt_Menu_Background);
    layout.putConstraint(SpringLayout.WEST, txt_Menu_Background, 0, SpringLayout.WEST, this);
    layout.putConstraint(SpringLayout.NORTH, txt_Menu_Background, 0, SpringLayout.NORTH, this);
    txt_Menu_Background.setPreferredSize(new Dimension(1600, 50));
    txt_Menu_Background.setEditable(false);
    txt_Menu_Background.setBackground(color_grey);

}

public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D)g;
    g2.fill(rect);


}


public void actionPerformed(ActionEvent e) {

    if (e.getSource() == btn_Quit)   {
        System.exit(0);
    }

}


public void windowOpened(WindowEvent e) {

}


public void windowClosing(WindowEvent e) {

}


public void windowClosed(WindowEvent e) {

}


public void windowIconified(WindowEvent e) {

}


public void windowDeiconified(WindowEvent e) {

}


public void windowActivated(WindowEvent e) {

}


public void windowDeactivated(WindowEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_W)    {
        rect.setLocation(rect.x, rect.y + 5);
    }

    else if (e.getKeyCode() == KeyEvent.VK_A)   {
        rect.setLocation(rect.x - 5, rect.y);
    }

    else if (e.getKeyCode() == KeyEvent.VK_D)   {
        rect.setLocation(rect.x + 5, rect.y);
    }

    else if (e.getKeyCode() == KeyEvent.VK_S)   {
        rect.setLocation(rect.x, rect.y - 5);
    }

    repaint();
}

@Override
public void keyReleased(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

2 个答案:

答案 0 :(得分:0)

Swing应该在自己的"事件派遣线程"。

中运行

请参阅:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

答案 1 :(得分:0)

KeyListener事件正在被Swing组件拦截,而不会被传递给您的侦听器。

最简单的解决方案是不要将Swing组件与您自己的" GUI"码;使用其中一种。