我在出错的地方注释了,一个java程序

houshen13828468384 2009-11-03 03:39:39
package drawTest1;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawTest1 {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
DrawSomeThingInterest picture=new DrawSomeThingInterest();//显示的是这行出错了 picture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
picture.setVisible(true);
}
});
}
class DrawSomeThingInterest extends JFrame
{
public DrawSomeThingInterest()
{
setTitle("interest pictures");
setSize(DEFAULT_WIDTH,DEFAULT_HIGHT);
DrawingRun gameforme=new DrawingRun();
add(gameforme);
}

public static final int DEFAULT_WIDTH=600;
public static final int DEFAULT_HIGHT=500;
}
}

class DrawingRun extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D) g;

double x1=10;
double y1=20;
double x2=100;
double y2=200;

Rectangle2D a1=new Rectangle2D.Double(x1,y1,x2,y2);
g2.draw(a1);

Ellipse2D ellipse=new Ellipse2D.Double();
ellipse.setFrame(a1);
g2.draw(ellipse);

g2.draw(new Line2D.Double(x1,y1,x1+x2,y1+y2));

double centerX=a1.getCenterX();
double centerY=a1.getCenterY();
double radius=200;

Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(centerX,centerY,centerX+radius,centerY+radius);
g2.draw(circle);
}
}




程序运行后显示:
Exception in thread "main" java.lang.Error: 无法解析的编译问题:
没有任何类型 DrawTest1 的外层实例可访问。必须用类型 DrawTest1 的外层实例(例如,x.new A(),其中 x 是 DrawTest1 的实例)来限定分配。

at drawTest1.DrawTest1.main(DrawTest1.java:14)


不知为何,希望指点
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
b_what_u_wanna_b_8 2011-06-26
  • 打赏
  • 举报
回复
come on 今天我也遇到这情况 原来是写错了大括号{ 包含成内部类了 话说内部类为什么会受限了?
houshen13828468384 2009-11-07
  • 打赏
  • 举报
回复
哦,这样啊
stamp80 2009-11-03
  • 打赏
  • 举报
回复
把DrawSomeThingInterest的类定义从DrawTest1当中移出来,不然的话它是内部类,访问受限。程序如下:
package drawTest1; 

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawTest1 {
public static void main(String[] args)
{
DrawTest1 t=new DrawTest1();
EventQueue.invokeLater(new Runnable()
{
public void run()
{
DrawSomeThingInterest picture=new DrawSomeThingInterest();//显示的是这行出错了
picture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
picture.setVisible(true);
}
});
}

}

class DrawSomeThingInterest extends JFrame
{
public DrawSomeThingInterest()
{
setTitle("interest pictures");
setSize(DEFAULT_WIDTH,DEFAULT_HIGHT);
DrawingRun gameforme=new DrawingRun();
add(gameforme);
}

public static final int DEFAULT_WIDTH=600;
public static final int DEFAULT_HIGHT=500;
}

class DrawingRun extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D) g;

double x1=10;
double y1=20;
double x2=100;
double y2=200;

Rectangle2D a1=new Rectangle2D.Double(x1,y1,x2,y2);
g2.draw(a1);

Ellipse2D ellipse=new Ellipse2D.Double();
ellipse.setFrame(a1);
g2.draw(ellipse);

g2.draw(new Line2D.Double(x1,y1,x1+x2,y1+y2));

double centerX=a1.getCenterX();
double centerY=a1.getCenterY();
double radius=200;

Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(centerX,centerY,centerX+radius,centerY+radius);
g2.draw(circle);
}
}

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧