简体   繁体   English

由于“线程“AWT-EventQueue-0”中的异常java.lang.UnsupportedOperationException:尚不支持,我的程序无法读取密钥。

[英]my program cant read the keys because of "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."

I am programming a snake game on Netbeans Java 8 and when I programmed the keys part, it didn't work because of Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet .我正在 Netbeans Java 8 上编程一个蛇游戏,当我对键部分进行编程时,它没有工作,因为Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yetException in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet

Can anyone tell me whats the solution or any other method I can use to control my snake specially in line 135 .谁能告诉我解决方案是什么,或者我可以用任何其他方法来控制我的蛇,特别是在第 135 行。

**package mainclass;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *

 */
public class gamepanel extends JPanel implements Runnable,KeyListener{


       private static final long serialversionid = 1L;

    public static final int WIDTH =500,HEIGHT =500;

    private Thread thread;

    private boolean  running;
    private boolean right =true,left=false,up=false,down=false;
    private bodyparts b;
    private ArrayList<bodyparts> snake;
    private int xcoor = 10,ycoor = 10, size= 5;
    private int ticks =0;


public gamepanel(){

    setFocusable(true);
    setPreferredSize(new Dimension(WIDTH,HEIGHT));
    addKeyListener(this);
    snake = new ArrayList<bodyparts>();
    start();


}
  public void start(){

           running = true;
           thread = new Thread(this);
           thread.start();
  }  
  public void stop(){

           try {
               running = false;
               thread.join();
           } catch (InterruptedException ex) {
               Logger.getLogger(gamepanel.class.getName()).log(Level.SEVERE, null, ex);
           }



  } 
  public void tick(){
      if (snake.size() == 0) {
          b = new bodyparts(xcoor,ycoor,10);
          snake.add(b);
      }
      ticks++;
      if(ticks>250000){
      if(right)xcoor++;
      if(left)xcoor--;
      if(up)ycoor--;
      if(down)ycoor++;
      ticks = 0;
      b = new bodyparts(xcoor,ycoor,10);
      snake. add (b);
      if(snake.size()>size){
          snake.remove(0);
          if(snake.size()>size){

          snake.remove(0);
          }
      }





  }
  }
  public void paint(Graphics g){
      g.setColor(Color.black);
      g.fillRect(0, 0, WIDTH, HEIGHT);



      for (int i = 0; i < WIDTH/10; i++) {
         g.drawLine(i*10, 0, i*10, HEIGHT);


      }
   for (int i = 0; i < WIDTH/10; i++) {
         g.drawLine(0,i*10, HEIGHT, i*10);


      }
      for (int i = 0; i < snake.size(); i++) {
          snake.get(i).draw (g);

      }

  }
  public void run(){
while (running){
repaint();
    tick();


}







}



   // @Override
    public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();
if(key==KeyEvent.VK_RIGHT&&!left){
right = true;
left=false;
up= false;
down = false;

}
if(key==KeyEvent.VK_LEFT&&!right){
right = false;
left=true;
up= false;
down = false;

}
if(key==KeyEvent.VK_UP&&!down){
right = false;
left=false;
up= true;
down = false;

}
if(key==KeyEvent.VK_DOWN&&!up){
right = false;
left=false;
up= false;
down = true;

}
    }

   // @Override
    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
     //@Override
    public void keyTyped(KeyEvent e) {


}

    }**

I expect the snake to be controlled, but its not and when I am trying to control it the is a big exception Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.我希望蛇被控制,但它不是,当我试图控制它时Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.一个大异常Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.

@SizeableShrimp has identified the cause of your exception. @SizeableShrimp 已确定您的异常原因。

When you implement KeyListener in an application class you need to provide implementations for the keyPressed and keyReleased method.当您在应用程序类中实现KeyListener ,您需要为keyPressedkeyReleased方法提供实现。 Apparently, you did this using an IDE to generate stub implementations.显然,您是使用 IDE 来生成存根实现的。

The problem is that the stub implementations won't necessarily work .问题是存根实现不一定有效 The IDE doesn't "know" what the methods mean ... or what they should actually do in your application. IDE 不“知道”这些方法的含义……或者它们在您的应用程序中实际应该做什么。 In this case, the IDE has inserted an implementation that is designed to throw an exception if it is called ... reminding you that you need to look at the code and implement the method properly.在这种情况下,IDE 插入了一个实现,该实现旨在在调用时抛出异常……提醒您需要查看代码并正确实现该方法。


In this case, it is really necessary to implement the keyRelease method properly because it will be called whenever you release a key that you previously pressed.在这种情况下,真的有必要正确实现keyRelease方法,因为每当您释放之前按下的键时都会调用它。

But the implementation is straightforward.但是实现很简单。 Really straightforward.真的很直接。 Your method doesn't to do anything at all, because key releases are not relevant to your game.您的方法根本不执行任何操作,因为密钥发布与您的游戏无关。

OR BETTER STILL : Do this a different way as per @MadProgrammer's comments!或者更好:按照@MadProgrammer 的评论以不同的方式执行此操作!


Lessons:课程:

  1. It is a good idea to read the javadocs for the interfaces that you add to your classes, so that you understand what needs to be implemented.阅读您添加到类中的接口的javadoc是一个好主意,以便您了解需要实现的内容。
  2. Read the code that the IDE completion wizards add for you.阅读 IDE 完成向导为您添加的代码。 They don't always get it right.他们并不总是正确的。 (They can't!) (他们不能!)
  3. When you get an exception, read the stacktrace.当您遇到异常时,请阅读堆栈跟踪。 It will tell you where the exception occurred.它会告诉您异常发生的位置。 Then look at the code ... and think about it.然后看代码……再想想。
  4. The message "Not supported yet."消息“尚不支持”。 is a big clue.是一个很大的线索。 It is saying that someone hasn't finished the job of coding something.这是说有人还没有完成编码的工作。 (In this case, the someone was you.) (在这种情况下,某人就是你。)

A programmers most important debugging tool is his/her brain, and the ability to put the clues together to diagnose problems.程序员最重要的调试工具是他/她的大脑,以及将线索放在一起以诊断问题的能力。 It takes practice.这需要练习。 My advice would be to start practicing!我的建议是开始练习!

public void keyReleased(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

This method is causing the problem.这种方法导致了问题。 Remove the line where it throws the exception and leave it empty.删除抛出异常的行并将其留空。 The instant you let go of a key this method is run and throws the exception.当你放开一个键时,这个方法就会运行并抛出异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ java.lang.UnsupportedOperationException:尚不支持。” - “java.lang.UnsupportedOperationException: Not supported yet.” “java.lang.UnsupportedOperationException:尚不支持。” - “java.lang.UnsupportedOperationException: Not supported yet.” Java Dropbox HttpServletRequest(java.lang.UnsupportedOperationException:尚不支持。) - Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: Not supported yet.) 正在执行操作的Java无法添加到arrayList,结果为“ AWT-EventQueue-0” java.lang.UnsupportedOperationException - Java in actionPerformed cannot add to arrayList, result is “AWT-EventQueue-0” java.lang.UnsupportedOperationException Java:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java-线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的Java异常java.lang.ArrayIndexOutOfBoundsException:1 - Java Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 1 线程“ AWT-EventQueue-0”中的java异常java.lang.ClassCastException - java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“ AWT-EventQueue-0”中的java异常java.lang.IllegalArgumentException - java exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException 线程““ AWT-EventQueue-0””中的Java异常java.lang.NullPointerException - Java Exception in thread '“AWT-EventQueue-0”' java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM