如何打电话给一个班级(在另一个班级内)

时间:2013-09-28 02:09:34

标签: java

所以我想运行我的代码中的“WASD”类:

public class MoveWASD extends JFrame
{
boolean Repeat = true;
int Location[] = {40, 40};
public static void main (String args[])
{
    new MoveWASD();
}

public MoveWASD() 
{
    super("Use-WASD-to-Move");
    setSize(800, 450);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    while(Repeat)
    {

- > WASD();         }

}

public void paint(Graphics g)
{
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 800, 450);

    g.setColor(Color.BLUE);
    g.fillRect(Location[0], Location[1], 20, 20);
}

public class WASD implements KeyListener
{





    public void keyPressed(KeyEvent event) 
    {
        if(event.getKeyChar() == 'w')
        {
            Location[1]--;
        }
        else if(event.getKeyChar() == 'd')
        {
            Location[0]++;
        }
        else if(event.getKeyChar() == 's')
        {
            Location[1]++;
        }
        else if (event.getKeyChar() == 'a')
        {
        Location[0]--;
        }
        else
        {
        Location[0] = Location[0];
        }
    }


    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }


}

}

箭头指向我的代码中的语法错误,显然当我输入WASD()时;它希望有一个名为WASD的方法,我需要做些什么才能找到CLASS WASD?

1 个答案:

答案 0 :(得分:1)

- > WASD();

这是对函数的调用。如果你想从MOVEASD调用类WASD,那么使用new运算符..

  

新的WASD();