不知道如何将计时器放入我的keylistener代码中

时间:2015-05-24 03:25:20

标签: java swing timer keylistener

基本上,我有一个keylistener代码(我知道键绑定谢谢你,但我不需要为我的项目),它会根据你按下的内容重新调整窗口框架的大小。但是,当你按住w时,它就像这样[暂停] wwwwwwww,每当你重新调整窗口大小时它都会引人注目。有人可以帮我成功地在我的代码中加入计时器吗?我已经排除了导入语句,但它们都在那里。

public class KeyFrame extends JFrame implements KeyListener
{
    public boolean t = true;
    private final HashSet<Integer> pressed = new HashSet<Integer>();

    private Timer timer;
    public KeyFrame(String name) {
        super(name);

    }

    public void keyTyped(KeyEvent e) {
        return;
    }

    public void keyPressed(KeyEvent e) {

        pressed.add(e.getKeyCode());

        new Timer(10, new ActionListener(){public void actionPerformed(ActionEvent arg0){}}).start(); 
        if (pressed.size() >= 1) {
            for(int code : pressed) {
                if(code == KeyEvent.VK_D){
                    this.setSize(getWidth()+5, getHeight());}
                if(code == KeyEvent.VK_A)
                    this.setSize(getWidth()-5, getHeight());
                if(code == KeyEvent.VK_S)
                    this.setSize(getWidth(), getHeight()+5);
                if(code == KeyEvent.VK_W)
                    this.setSize(getWidth(), getHeight()-5);
            }
        }
    }


    public void keyReleased(KeyEvent e) {
        pressed.remove(e.getKeyCode());
    }
}

1 个答案:

答案 0 :(得分:1)

  

然而,当你按住w时,它就像这样[暂停] wwwwwwww

这是大多数操作系统的标准操作

不要在Timer中启动KeyPressed,让Timer独立于KeyListener

运行

您可能还希望将代码检查放入ActionListener而不是keyPressed方法

public class KeyFrame extends JFrame implements KeyListener
{
    public boolean t = true;
    private final HashSet<Integer> pressed = new HashSet<Integer>();

    private Timer timer;
    public KeyFrame(String name) {
        super(name);

        new Timer(10, new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                if(pressed.contains(KeyEvent.VK_D)) {
                    setSize(getWidth()+5, getHeight());
                } else if(pressed.contains(KeyEvent.VK_A)) {
                    setSize(getWidth()-5, getHeight());
                } else if(pressed.contains(KeyEvent.VK_S)) {
                    setSize(getWidth(), getHeight()+5);
                } else if(pressed.contains(KeyEvent.VK_W)) {
                    setSize(getWidth(), getHeight()-5);
                }
            }
        }).start(); 
     }

    public void keyTyped(KeyEvent e) {
        return;
    }

    public void keyPressed(KeyEvent e) {
        pressed.add(e.getKeyCode());
    }

    public void keyReleased(KeyEvent e) {
        pressed.remove(e.getKeyCode());
    }
}

您还会发现密钥绑定将解决与KeyListener相关的焦点相关问题,这就是我们通常推荐的原因

  

我知道keybindings谢谢你,但我不需要那个项目

然后你应该使用它们,真的没有任何借口不

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
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.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Delta xDelta;
        private Delta yDelta;

        public TestPane() {
            xDelta = new Delta();
            yDelta = new Delta();
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new DeltaAction(yDelta, -5));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.released", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), new DeltaAction(yDelta));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new DeltaAction(yDelta, 5));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.released", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), new DeltaAction(yDelta));

            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), new DeltaAction(xDelta, -5));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.released", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), new DeltaAction(xDelta));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), new DeltaAction(xDelta, 5));
            bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.released", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), new DeltaAction(xDelta));

            Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Window window = SwingUtilities.getWindowAncestor(TestPane.this);
                    Dimension size = window.getSize();
                    size.width += xDelta.getValue();
                    size.height += yDelta.getValue();
                    window.setSize(size);
                }
            });
            timer.start();
        }

        public void bindKeyStrokeTo(int condition, String name, KeyStroke keyStroke, Action action) {
            InputMap im = getInputMap(condition);
            ActionMap am = getActionMap();

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

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

    }

    public class Delta {

        private int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

    }

    public class DeltaAction extends AbstractAction {

        private Delta delta;
        private int value;

        public DeltaAction(Delta delta, int value) {
            this.delta = delta;
            this.value = value;
        }

        public DeltaAction(Delta delta) {
            this(delta, 0);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            delta.setValue(value);
        }

    }

}