不确定如何在我的侦听器代码中添加计时器 [英] Not sure how to put timer into my keylistener code

查看:70
本文介绍了不确定如何在我的侦听器代码中添加计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个键侦听器代码(我知道键绑定非常感谢,但我的项目不需要该键),它将根据您所按的内容重新调整窗口框架的大小.但是,当您按住w时,它会像这样[暂停] wwwwwwww,并且在您重新调整窗口大小时会很明显.有人可以帮助我在代码中成功添加计时器吗?我已经排除了导入语句,但是它们都在那里.

basically, I have a keylistener code (I am aware of keybindings thank you but I don't need that for my project), and it will re-size the window frame based on what you press. However, when you hold down w, it goes like this w [pause] wwwwwwww and it's noticeable whenever you re-size the window. Can someone help me successfully put timer in my code? I've excluded the import statements but they're all there.

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());
    }
}

推荐答案

但是,当您按住w时,它会像这样[暂停] wwwwwwww

However, when you hold down w, it goes like this w [pause] wwwwwwww

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

This is pretty standard operation under most OS's

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

Don't start the Timer in KeyPressed, have the Timer running independently of the KeyListener

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

You might also like to put the check for the code into the ActionListener instead of the keyPressed method

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相关的与焦点相关的问题,这就是我们通常推荐的原因

You will also find the Key Bindings will solve the focus related issues associated with KeyListener which is why we typically recommend it

我知道按键绑定,谢谢,但是我的项目不需要它

I am aware of keybindings thank you but I don't need that for my project

那么您应该使用它们,真的没有任何借口可以

Then you should use them, there really isn't any excuse not to

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);
        }

    }

}

这篇关于不确定如何在我的侦听器代码中添加计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆