2016-02-05 65 views
0

我試圖創建一個程序,其中一個對象不斷跌落在屏幕上,除非用戶按下空格,在這種情況下它會向上移動。到目前爲止,當我運行程序時,我已經得到了最初開始下降的對象。當我按下空間時,對象向上移動(就像它應該)。但是,當我釋放鍵時,對象停止移動。我希望它在鑰匙被釋放時繼續下降。java keyylisterner幫助對象在鍵釋放後移動

我看過使用鍵綁定而不是keylistener,但我遇到了麻煩。我是一名高中學生,主修計算機科學,所以原諒我不知道太多先進的編碼術語/方法。然而,我是我班上最好的學習者之一,也是一位渴望解決這個問題的快速學習者。這裏是我的代碼:

public class htestnew extends JPanel implements ActionListener, KeyListener { 
    Timer t = new Timer(5, this); 
    int x = 20, y = 20, vely = 1; 

    public htestnew() { 
     t.start(); 
     addKeyListener(this); 
     setFocusable(true); 
     setFocusTraversalKeysEnabled(false); 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     ImageIcon background = new ImageIcon("OBJECTEXAMPLE1"); 
     background.paintIcon(this, g, 0, 0); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (y < 0) 
     { 
      vely = 0; 
      y = 0; 
     } 

     if (y > 305) //-70 
     { 
      vely = 0; 
      y = 305; 
     } 

     y += vely; 
     repaint(); 
    } 

    public void keyPressed(KeyEvent e) { 
     int code = e.getKeyCode(); 

     if (code == KeyEvent.VK_SPACE) { 
      vely = -1; 
     } 
    } 

    public void keyTyped(KeyEvent e) { 
    } 

    public void keyReleased(KeyEvent e) { 
     vely = 0; 
     //I have tried setting this value to 1 but it does not work 
    } 

如果它更容易使用鍵綁定個人和易於遵循的例子將是驚人的。謝謝。

+0

唉唉,你的代碼不實際的油漆什麼... – MadProgrammer

回答

2

所以,改變...

public void keyReleased(KeyEvent e) { 
vely=0; 
//I have tried setting this value to 1 but it does not work 
} 

到...

public void keyReleased(KeyEvent e) { 
vely=1; 
//I have tried setting this value to 1 but it does not work 
} 

基本上使工作

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test extends JPanel implements ActionListener, KeyListener { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new Test()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    Timer t = new Timer(5, this); 
    int x = 20, y = 20, vely = 1; 

    public Test() { 
     t.start(); 
     addKeyListener(this); 
     setFocusable(true); 
     setFocusTraversalKeysEnabled(false); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(300, 300); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawRect(x, y, 5, 5); 
    } 

    public void actionPerformed(ActionEvent e) { 

     if (y < 0) { 
      vely = 0; 
      y = 0; 
     } 

     if (y > 305) //-70 
     { 
      vely = 0; 
      y = 305; 
     } 

     y += vely; 
     repaint(); 
    } 

    public void keyPressed(KeyEvent e) { 
     int code = e.getKeyCode(); 

     if (code == KeyEvent.VK_SPACE) { 
      vely = -1; 
     } 

    } 

    public void keyTyped(KeyEvent e) { 
    } 

    public void keyReleased(KeyEvent e) { 
     vely = 1; 
//I have tried setting this value to 1 but it does not work 
    } 
} 

我(我的東西,實際上油漆添加後)強烈建議你看看How to Use Key Bindings而不是用KeyListener,它會解決th E核問題KeyListener

鍵綁定實例...

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test extends JPanel implements ActionListener { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new Test()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    Timer t = new Timer(5, this); 
    int x = 20, y = 20, vely = 1; 

    public Test() { 
     t.start(); 

     addKeyBinding("space.pressed", KeyEvent.VK_SPACE, true, new ChangeYAction(-1)); 
     addKeyBinding("space.released", KeyEvent.VK_SPACE, false, new ChangeYAction(1)); 
    } 

    protected void addKeyBinding(String name, int virtualKey, boolean pressed, Action action) { 
     addKeyBinding(name, KeyStroke.getKeyStroke(virtualKey, 0, !pressed), action); 
    } 

    protected void addKeyBinding(String name, KeyStroke keyStroke, Action action) { 
     InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); 
     ActionMap am = getActionMap(); 

     im.put(keyStroke, name); 
     am.put(name, action); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(300, 300); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawRect(x, y, 5, 5); 
    } 

    public void actionPerformed(ActionEvent e) { 

     if (y < 0) { 
      vely = 0; 
      y = 0; 
     } 

     if (y > 305) //-70 
     { 
      vely = 0; 
      y = 305; 
     } 

     y += vely; 
     repaint(); 
    } 

    public class ChangeYAction extends AbstractAction { 

     private int changeYTo; 

     public ChangeYAction(int changeYTo) { 
      this.changeYTo = changeYTo; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      vely = changeYTo; 
     } 

    } 

} 
+0

非常感謝你 – BSmyth