[英]Adding a main class to an applet
I added a main method to a Java applet in order to run it as an application but it requires me to initialize all the methods in the class that contains the main. 我向Java applet添加了main方法,以便将其作为应用程序运行,但它要求我初始化包含main的类中的所有方法。 I managed to initialize the
init
method but I have failed to initialize all methods that carry on arguments. 我设法初始化了
init
方法,但未能初始化所有带有参数的方法。
Any one with an idea on how to proceed? 有人对如何进行有任何想法吗?
Here is code from a working application. 这是正在运行的应用程序中的代码。 Note how the applet methods are called:
注意如何调用applet方法:
JFrame frame = new JFrame();
frame.setSize(400, 300);
final Applet applet = new MyCustomApplet();
frame.getContentPane().add(applet);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
applet.stop();
applet.destroy();
System.exit(0);
}
});
frame.setVisible(true);
applet.init();
applet.start();
There is more to starting some applets than simply calling the start()
and init()
methods. 除了简单地调用
start()
和init()
方法之外,启动一些applet的功能还更多。 Many applets require a valid AppletContext
and AppletStub
in order to work correctly. 许多小程序都需要有效的
AppletContext
和AppletStub
才能正常工作。
The best strategy would be to factor the GUI into a separate class that is added either to an applet or frame as needed. 最好的策略是将GUI分解为一个单独的类,然后根据需要将其添加到applet或框架中。 This is called an 'hybrid application/applet'.
这称为“混合应用程序/小程序”。 Subway is a good example of an hybrid, though it does not accept arguments.
尽管Subway不接受参数,但它是混合动力的一个很好的例子。 For the arguments, accept them in the constructor of the GUI class, or include get/set methods for them.
对于参数,请在GUI类的构造函数中接受它们,或为它们包含get / set方法。 The applet would use
getParam(String)
to determine what values to use, while the application would get the arguments from the String[]
from main(String[] args)
. 小程序将使用
getParam(String)
,以确定哪些值使用,而应用会得到从参数String[]
从main(String[] args)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.