2012-02-06 103 views
0

我得到的java:105:错误:非法字符:\ 29 }为什么我得到{error?

我写一个程序,用户可以点击左/右/上/下按钮,移动“球”在屏幕上。

我搞不​​清楚我做错了什么。有人可以帮助我吗?

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Lab2a extends JFrame { 

Lab2a(){ 
    setTitle("Lab 1b - Application #2"); 
    Lab2Panel p = new Lab2Panel(); 
    add(p); 
} 

public static void main(String[] args){ 

    Lab2 frame = new Lab2(); 
    frame.setTitle("Lab2 Application # 1"); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(600, 400); 
    frame.setVisible(true); 
    } 

} 

class Lab2Panel extends JPanel{ 
Lab2Button canvas = new Lab2Button(); 
JPanel panel = new JPanel(); 

Lab2Panel() { 

    setLayout(new BorderLayout()); 

    JButton leftButton = new JButton("left"); 
    JButton rightButton = new JButton("right"); 
    JButton upButton = new JButton("up"); 
    JButton downButton = new JButton("down"); 

    panel.add(leftButton); 
    panel.add(rightButton); 
    panel.add(upButton); 
    panel.add(downButton); 

    this.add(canvas, BorderLayout.CENTER); 
    this.add(panel, BorderLayout.SOUTH); 

    leftButton.addActionListener(new LeftListener(canvas)); 
    rightButton.addActionListener(new RightListener(canvas)); 
    upButton.addActionListener(new UpListener(canvas)); 
    downButton.addActionListener(new DownListener(canvas)); 
} 


} 

class Lab2Button extends JPanel { 
int radius = 5; 
int x = -1; 
int y = -1; 

protected void paintComponent(Graphics g){ 
    if (x<0 || y<0) { 
     x = getWidth()/2 - radius; 
     y = getHeight()/2 - radius; 
    } 
    super.paintComponent(g); 
    g.drawOval(x,y, 2 * radius, 2 * radius); 

} 

     public void moveLeft(){ 

      x -= 5; 
      this.repaint(); 
     } 

     public void moveRight(){ 

      x += 5; 
      this.repaint(); 
     } 

     public void moveUp(){ 
      y += 5; 
      this.repaint(); 
     } 

     public void moveDown(){ 
      y -= 5; 
      this.repaint(); 
     } 


} 

class LeftListener implements ActionListener{ 
    private Lab2Button canvas; 

    LeftListener(Lab2Button canvas) { 
    this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
    canvas.moveLeft(); 
    } 
} 

对不起,关于105是这一条上面的线。

class RightListener implements ActionListener{ 
    private Lab2Button canvas; 

    RightListener(Lab2Button canvas) { 
     this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
     canvas.moveRight(); 
    } 
} 


class UpListener implements ActionListener{ 
    private Lab2Button canvas; 

    UpListener(Lab2Button canvas) { 
     this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
     canvas.moveUp(); 
    } 
} 



class DownListener implements ActionListener{ 
    private Lab2Button canvas; 

    DownListener(Lab2Button canvas) { 
     this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
    canvas.moveDown(); 
    } 
} 
+3

你能告诉我们哪一行是105行吗? – ggreiner 2012-02-06 18:39:23

+1

哪一行是105行? – templatetypedef 2012-02-06 18:39:32

+0

仔细阅读错误信息,尝试理解它,并仔细缩进代码。 – 2012-02-06 18:44:02

回答

2

因此,看起来有几个问题,但不完全如您所述。

  1. 线15 Lab2 frame = new Lab2();想必应该是Lab2a frame = new Lab2a();,或者你错过了,包括您的Lab2对象的声明。

  2. 一旦问题1解决,代码编译好。这意味着错误发生在两个地方之一。

    1. 您可能排除的Lab2声明。

    2. 你的源文件的字节,在这种情况下,最好的想法是从另一个源代码(如StackOverflow)删除并重新粘贴到代码中,或者更好地重新输入代码。你可以改善沿途:)

+0

谢谢我不得不用不同的名字重新输入代码谢谢 – Robert 2012-02-06 18:59:14

1

没有行号很难猜测的格式,但它看起来像你有两个右括号在这里,你不应该:

public static void main(String[] args){ 

    Lab2 frame = new Lab2(); 
    frame.setTitle("Lab2 Application # 1"); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(600, 400); 
    frame.setVisible(true); 
    } <--- EXTRA 

} 

更新:如果你的意图是一个类,其余的是内部类,那么标记在上面的大括号应该移到文件的底部。

+4

起初我也这么想过,但那是关闭'Lab2a'类的支架 – 2012-02-06 18:45:29

+0

在我做其他类之前关闭了类声明 – Robert 2012-02-06 18:47:02

+1

这就是我第一次想到的,但它只是不好的缩进。你指出的那个关闭了下一个关闭类的方法。 – Shaded 2012-02-06 18:47:33

1

能不明白的地方是错误..

public static void main(String[] args){ 

    Lab2 frame = new Lab2(); 
} 

你的意思是Lab2a在这个代码?

+1

是的,我错过了,但它没有修复它 – Robert 2012-02-06 18:46:43

+0

我编辑了代码以显示105行。我计算了大括号,但仍然没有看到我做错了什么 – Robert 2012-02-06 18:48:13

+1

尝试将另一个全局类放在另一个文件中或使其成为内部类 – alaster 2012-02-06 18:49:40

2

我试着用你的代码,简单地改变(第20行):

Lab2 frame = new Lab2(); 

Lab2a frame = new Lab2a(); 

有它无差错的工作在我的机器上..负的事实,向上和向下的反转:P

编辑:另外NetBeans自动解析您的导入为:

import java.awt.BorderLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

从你有什么,这可能起到了一部分让它为我工作。

+0

中使用IDE好了废话还是给我一个错误 – Robert 2012-02-06 18:51:58

+1

@ user512915你可能有一个隐藏的字符搞乱了那一行..尝试删除它,上面和下面的行并重新输入它们,因为它对我来说是完美的。 – Alex 2012-02-06 18:55:47

+0

好的,谢谢我们不应该在课堂上只使用IDE的文本板 – Robert 2012-02-06 18:56:29

相关问题