有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java KeyListener无法访问keyPressed方法

目前正在尝试制作一个小游戏,我在移动我制作的立方体时遇到问题,经过调试,当我按w、a、s或d时,它不会移动到按键方法,当我的应用程序打开时,我也遇到了一个问题。在我将鼠标悬停在按钮上之前,我放置的按钮不会出现,而且我的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) 个答案

  1. # 2 楼答案

    KeyListener事件被Swing组件截获,而不是传递给您的侦听器

    最简单的解决方案是不要将Swing组件和您自己的“GUI”代码组合在一起;使用一个或另一个