prprprll
2020-06-09 16:21
采纳率: 100%
浏览 175
已采纳

想请大家看一下错在哪里呀,下面的是eclipse的错误提示

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class MyGame extends JFrame {
public MyGame(){
this.setTitle("我的游戏");
this.setBounds(300,100, 800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.black);

MyPanel mp=new MyPanel();
add(mp);
this.addKeyListener(mp) ;
mp.addKeyListener(mp);

Thread t=new Thread();
t.start();
}
public static void main(String args[]){
new MyGame();
}
}
class MyPanel extends JPanel implements Runnable,KeyListener{
int score;
int ballcount=8;

int x[] = new int[ballcount];
int y[] = new int[ballcount];
char ch[]=new char[ballcount];
Color ballColor[]=new Color[ballcount];
public void init(){
score=20;
for (int i = 0; i < ballcount; i++) {
x[i] = (int) (Math.random() * 800);
y[i] = (int) (Math.random() * 600);

ch[i]=(char)(Math.random()*26+65);

ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
}
public MyPanel(){
init();
}

public void paint(Graphics g){
Image img;
super.paint(g);
try {
img = ImageIO.read(new File("19.jpg"));
g.drawImage(img, 0, 0, this);

} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();
}
g.setFont(new Font("", Font.BOLD, 28));
g.setColor(Color.red);
g.drawString("score:"+score, 10, 30);
for (int i = 0; i < ballcount; i++) {
g.setColor(ballColor[i]);
g.fillOval(x[i] - 20, y[i] - 30, 50, 50);
g.setFont(new Font("", Font.BOLD, 20));
g.setColor(Color.white);
g.drawString(""+ch[i], x[i], y[i]);
y[i]++;
if (y[i] > 600) {
y[i] = 0;
x[i] = (int) (Math.random() * 1000);
ch[i]=(char)(Math.random()*26+65);
ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
score--;
}
}
}

public void run() {
while (true) {
repaint();
try {
Thread.sleep(10);
}catch (InterruptedException e) {
e.printStackTrace();
}
}

}

public void keyPressed(KeyEvent e) {
char c=e.getKeyChar();
System.out.println(c);
for(int i=0;i<ballcount;i++){
if(c==ch[i]){ x[i]=(int)(Math.random()*800);
y[i]=0;
ch[i]=(char)((Math.random()*26)+65);
ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
score++;
}
}
}

public void keyReleased(KeyEvent e) { }

public void keyTyped(KeyEvent e) { }
}
错误是
MyGame.java:47: 错误: 非法的表达式开始
public MyPanel(){

eclipse的错误显示
Syntax error, insert "}" to complete Block

at game.MyPanel.init(MyGame.java:46)
at game.MyPanel.<init>(MyGame.java:49)
at game.MyGame.<init>(MyGame.java:21)
at game.MyGame.main(MyGame.java:29)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2020-06-09 16:26
    最佳回答

    public void init(){
    score=20;
    for (int i = 0; i < ballcount; i++) {
    x[i] = (int) (Math.random() * 800);
    y[i] = (int) (Math.random() * 600);

    ch[i]=(char)(Math.random()*26+65);

    ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
    }
    //少了一个后花括号
    }

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题