有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么会出现{错误?

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

共 (4) 个答案

  1. # 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
    
    }
    

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

  2. # 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;
    

    从你所拥有的来看,这可能是我成功的原因之一

  3. # 3 楼答案

    因此,似乎存在一些问题,但与您描述的不完全一样

    1. 第15行Lab2 frame = new Lab2();可能应该是Lab2a frame = new Lab2a();,或者您没有包含Lab2对象的声明

    2. 问题1解决后,代码就可以正常编译了。这意味着错误在两个位置之一

      1. 您可能排除的Lab2声明

      2. 源文件的字节数,在这种情况下,最好是从另一个源文件(如StackOverflow)中删除并重新粘贴代码,或者最好重新键入代码。您可以在此过程中改进格式:)

  4. # 4 楼答案

    无法理解错误在哪里

    public static void main(String[] args){
    
        Lab2 frame = new Lab2();
    }
    

    你是说这个代码中的Lab2a