Derzeit versucht, ein kleines Spiel zu machen, und ich habe ein Problem mit dem Verschieben des Cubes, den ich gemacht habe, ging durch Debuggen und wenn ich w, a, s oder d drücken ist es nicht Verschieben auf die KeyPressed-Methode, ich habe auch ein Problem, wenn meine Anwendung öffnet die Schaltflächen, die ich platziert habe erscheinen nicht, bis ich über sie schweben und auch meine JTextField überhaupt nicht angezeigt wird, würde Hilfe sehr geschätzt werdenKeyListener nicht KeyPressed Methode
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.
}
}
Wo liegt der Fokus, wenn Sie die Taste drücken? – bradimus