当前位置: 代码迷 >> J2SE >> The public type MyPanel must be defined in its own file,该如何处理
  详细解决方案

The public type MyPanel must be defined in its own file,该如何处理

热度:4971   发布时间:2013-02-25 21:54:29.0
The public type MyPanel must be defined in its own file
本人在学习韩老师的视频时遇到一个错误,求高手解救。
代码:
/*
 * 功能:坦克游戏的1.0版
 * 1.画出坦克
 */
package com;
import javax.swing.*;
import java.awt.*;
public class MyTankGame1 extends JFrame{

MyPanel mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
MyTankGame1 mtg=new MyTankGame1();
}

//构造函数
public MyTankGame1()
{
mp=new MyPanel();

this.add(mp);
this.setSize(400,300);
this.setVisible(true);
}

}

//我的面板
public class MyPanel extends JPanel
{
//定义一个我的坦克
Hero hero=null;

//构造函数
public MyPanel()
{
hero=new Hero(10,10);
}

//重新paint
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
g.setColor(Color.CYAN);
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的矩形
g.fill3DRect(hero.getX(), hero.getY(), 5, 30,false);
//2.画出右边的矩形
g.fill3DRect(hero.getX()+25, hero.getY(), 5, 30,false);
//3.画出中间的矩形
g.fill3DRect(hero.getX()+5, hero.getY()+5, 20, 20,false);
//4.画出中间的圆形
g.fillOval(hero.getX()+5, hero.getY()+5, 20, 20);
}
}

//坦克类
class Tank
{
//表示坦克的横坐标
int x=0;
//坦克纵坐标
int y=0;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}

}

//我的坦克
class Hero extends Tank
{
public Hero(int x,int y)
{
super(x,y);
}
}



错误:Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The public type MyPanel must be defined in its own file


有高手能指点迷津不?
public的类必须放在单独的文建里,且文件名必须与public类名相同

一个文件里只能有一个public类改成内部类吧~~把public 去掉 或者改成MyTankGame1内部类把下面的public 去掉


//我的面板
public class MyPanel extends JPanel

  相关解决方案