簡體   English   中英

為什么會收到{錯誤?

[英]Why am I getting a { error?

我正在獲取Java:105:錯誤:非法字符:\\ 29}

我正在編寫一個程序,用戶可以在其中單擊右/左/上/下按鈕,然后在屏幕上移動“球”。

我不知道自己在做什么錯。 有人可以幫我嗎?

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();
    }
}

因此,似乎存在一些問題,但與您所描述的不完全相同。

  1. 第15行Lab2 frame = new Lab2(); 大概應該是Lab2a frame = new Lab2a(); ,或者您錯過了對Lab2對象的聲明。

  2. 問題1解決后,代碼即可正常編譯。 這意味着錯誤位於2個位置之一。

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

    2. 源文件的字節,在這種情況下,最好的辦法是從另一個源(例如StackOverflow)中刪除並重新粘貼代碼,或者更好地重新鍵入代碼。 您可以沿途改進格式:)

我嘗試使用您的代碼並簡單地更改(第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;

從您的經驗來看,這可能對讓它為我發揮了作用。

沒有行號,很難猜測,但是看起來您在這里有兩個不應該的括號:

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

}

更新:如果您的意圖是一門課,其余的是內部課,那么上面標有多余的大括號應移到文件的底部。

無法理解錯誤在哪里。

public static void main(String[] args){

    Lab2 frame = new Lab2();
}

您是說這段代碼中的Lab2a嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM