java程序编译后类找不到主方法
运行后的错误: 在类 WithinRectangle$PaintPanel 中找不到主方法, 请将主方法定义为:
public static void main(String[] args)
代码如下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WithinRectangle extends JFrame{
private PaintPanel paintPanel = new PaintPanel();
public WithinRectangle() {
add(paintPanel);
}
/** 主方法 */
public static void main(String[] args) {
JFrame frame = new WithinRectangle();
frame.setTitle("Inside the rectangle?");
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// 内部类
static class PaintPanel extends JPanel{
private MyRectangle2D rectangle ;
private boolean isInside = false;
private Point mousePoint = new Point(0,0); //鼠标默认点
public PaintPanel(){
WithinRectangle a=new WithinRectangle();
rectangle= new MyRectangle2D(100,60,100,40);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMove(MouseEvent e){
if(rectangle.contains(e.getX(), e.getY()))
isInside = true;
else
isInside = false;
mousePoint.x = e.getX();
mousePoint.y = e.getY();
repaint();
}
});
}
/** 画矩行和字符串 */
public void paintComponent(Graphics g){
super.paintComponent(g);
//绘制矩形
g.drawRect((int)(rectangle.getX()) - (int)(rectangle.getWidth() / 2),
(int)(rectangle.getY()) - (int)(rectangle.getHeight() / 2),
(int)(rectangle.getWidth()), (int)(rectangle.getHeight()));
//绘制字符串
g.drawString(isInside ? "Mouse point is in the rectangle" :
"Mouse point is not in the rectangle",
mousePoint.x, mousePoint.y);
}
/** 2D矩形类 */
class MyRectangle2D {
//矩形的中心点坐标
private double x;
private double y;
//矩形的长和宽
private double width;
private double height;
// 无参构造函数
public MyRectangle2D(){
x = 0;
y = 0;
width = 1;
height = 1;
}
// 构造方法
public MyRectangle2D(double x,double y,double width,double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public double getAreac(){
return width * height;
}
public double getPerimeter(){
return (width + height) * 2;
}
public void setX(double newX) {
x = newX;
}
public void setY(double newY) {
y = newY;
}
public void setWidth(double width){
this.width = width;
}
public void setHeight(double height){
this.height = height;
}
public boolean contains(double x,double y){
if( (this.x - width/2) < x && (this.x + width/2) > x &&
(this.y - height/2) < y && (this.y + height/2) > y)
return true;
else
return false;
}
}
}
}
请问一般在主类中引用别的类,那个另外定义的类是放在主类中?