當前嘗試製作一個小遊戲,我在移動我製作的多維數據集時遇到問題,經歷了調試,當我按w時,a,s或d它不是移動到keyPressed方法,我也有一個問題,當我的應用程序打開我放置的按鈕不會出現,直到我將鼠標懸停在他們身上,而且我的JTextField根本不顯示,幫助將不勝感激KeyListener沒有達到keyPressed方法
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.
}
}
按鍵時焦點在哪裏? – bradimus