Java Thread只能运行一次

时间:2016-06-16 16:00:20

标签: java multithreading object move keylistener

我有一个带有文本的JLabel,我想用KeyListener移动。 当我按下箭头键时,标签应该移动到给定的方向。

奇怪的是,它只能按时运作,之后我会得到一个很长的例外:

"Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Unknown Source)
    at Collatz$2.keyPressed(Collatz.java:64)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这是我的代码的一部分,有人有想法吗? 我真的不是那种线索......所以也许我的尝试是错的?

    field = new JLabel("Test");
    add(field);

    Thread t = new Thread(new Runnable(){
        @Override
        public void run(){                        
            System.out.println("In Thread");
            field.setBounds(x_pos,pos_y,50,50);                
        }
    });

    addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) { }

        @Override
        public void keyPressed(KeyEvent e) {  
            // System.out.println("Code: " + e.getKeyCode());

            if (e.getKeyCode() == 27){
                System.exit(0);
            }

            if (e.getKeyCode() == 37){
                pos_x--;
                t.start();
            }

            if (e.getKeyCode() == 38){
                pos_y--;
                t.start();
            }

            if (e.getKeyCode() == 39){
                pos_x++;
                t.start();
            }

            if (e.getKeyCode() == 40){
                pos_y++;
                t.start();
            }                

        }

        @Override
        public void keyReleased(KeyEvent e) {}
    });

    setVisible(true);  

0 个答案:

没有答案