当前位置: 技术问答>java相关
一个java初级问题
来源: 互联网 发布时间:2015-01-07
本文导语: 源代码如下: import java.applet.Applet; import java.awt.Event; import java.awt.Frame; import java.awt.Label; import java.awt.event.*; public class StarterCombined extends Applet { private Label label; public static void main(String args[]) { StarterCombinedF...
源代码如下:
import java.applet.Applet;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.*;
public class StarterCombined extends Applet
{
private Label label;
public static void main(String args[])
{
StarterCombinedFrame app=new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show();
System.out.println("StarterCombinedFrame:main()");
}
public void init()
{
System.out.println("Applet:init()");
}
public void start()
{
Label label=new Label();
add(label);
System.out.println("Applet:strat()");
}
public void stop()
{
System.out.println("Applet:stop()");
}
public void destory()
{
System.out.println("Applet:destory()");
}
class StarterCombinedFrame extends Frame
{
StarterCombinedFrame(String frameTitle)
{
super(frameTitle);
StarterCombined applet=new StarterCombined();
applet.start();
add(applet,"Center");
addWindowListener(new windowAdapter()
{
public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
});
}
}
}
错误消息如下:
D:jdk1.2test>javac StarterCombined.java
StarterCombined.java:12: No enclosing instance of class StarterCombined is in sc
ope; an explicit one must be provided when creating inner class StarterCombined.
StarterCombinedFrame, as in "outer. new Inner()" or "outer. super()".
StarterCombinedFrame app=new StarterCombinedFrame("Starter Appli
cation");
^
1 error
那位高手能指点一下呀,谢谢。
import java.applet.Applet;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.*;
public class StarterCombined extends Applet
{
private Label label;
public static void main(String args[])
{
StarterCombinedFrame app=new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show();
System.out.println("StarterCombinedFrame:main()");
}
public void init()
{
System.out.println("Applet:init()");
}
public void start()
{
Label label=new Label();
add(label);
System.out.println("Applet:strat()");
}
public void stop()
{
System.out.println("Applet:stop()");
}
public void destory()
{
System.out.println("Applet:destory()");
}
class StarterCombinedFrame extends Frame
{
StarterCombinedFrame(String frameTitle)
{
super(frameTitle);
StarterCombined applet=new StarterCombined();
applet.start();
add(applet,"Center");
addWindowListener(new windowAdapter()
{
public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
});
}
}
}
错误消息如下:
D:jdk1.2test>javac StarterCombined.java
StarterCombined.java:12: No enclosing instance of class StarterCombined is in sc
ope; an explicit one must be provided when creating inner class StarterCombined.
StarterCombinedFrame, as in "outer. new Inner()" or "outer. super()".
StarterCombinedFrame app=new StarterCombinedFrame("Starter Appli
cation");
^
1 error
那位高手能指点一下呀,谢谢。
|
不好意思,应该是这样的:
public static void main(String args[])
{
StarterCombined myapplet=new StarterCombined();
StarterCombinedFrame app=myapplet.new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show();
System.out.println("StarterCombinedFrame:main()");
}
public static void main(String args[])
{
StarterCombined myapplet=new StarterCombined();
StarterCombinedFrame app=myapplet.new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show();
System.out.println("StarterCombinedFrame:main()");
}
|
import java.applet.Applet;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.*;
public class StarterCombined extends Applet
{
private Label label;
public static void main(String args[])
{
StarterCombinedFrame app=new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show();
System.out.println("StarterCombinedFrame:main()");
}
public void init()
{
System.out.println("Applet:init()");
}
public void start()
{
Label label=new Label("sdf");
add(label);
System.out.println("Applet:strat()");
}
public void stop()
{
System.out.println("Applet:stop()");
}
public void destory()
{
System.out.println("Applet:destory()");
}
}
class StarterCombinedFrame extends Frame
{
StarterCombinedFrame(String frameTitle)
{
super(frameTitle);
StarterCombined applet=new StarterCombined();
applet.start();
add(applet,"Center");
/*
addWindowListener(new windowAdapter()
{
public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
});
*/
}
}
|
public static void main(String args[])
{
StarterCombinedFrame app=new StarterCombinedFrame("Starter Application");
}
这一句有问题,starterCombinedFrame 是一个inner class,在它的outer实例没有被创建之前是不能new的
{
StarterCombinedFrame app=new StarterCombinedFrame("Starter Application");
}
这一句有问题,starterCombinedFrame 是一个inner class,在它的outer实例没有被创建之前是不能new的
|
是Inner Class的问题,
应该先创建outer class的实例,
在创建Inner Class的实例。
可能应该是这样吧:StarterCombinedFrame app=new StarterCombined().getinstance().StarterCombinedFrame("Starter Appli");
具体怎么样我也记不太清了,反正应该是这个问题。你可已查查Jdk Doc
应该先创建outer class的实例,
在创建Inner Class的实例。
可能应该是这样吧:StarterCombinedFrame app=new StarterCombined().getinstance().StarterCombinedFrame("Starter Appli");
具体怎么样我也记不太清了,反正应该是这个问题。你可已查查Jdk Doc