2012-02-06 114 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

相關問題