在小程序中使用 main 方法是否合适? [英] Is it ever appropriate to have a main method in an applet?

查看:19
本文介绍了在小程序中使用 main 方法是否合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解小程序的使用规则.我意识到小程序不必有一个 main 方法(它可以使用 init() 代替),但我教授提供的每个代码示例都是一个带有 main 方法的小程序,如下面的代码所示.

I am trying to wrap my head around the rules of applet use. I realize that an applet does not have to have a main method (it can use init() instead) but every code sample my professor provides is an applet with a main method, like in the code below.

我的理解是,由于安全问题,在小程序中使用 main 方法不是一个好主意,这是正确的吗?什么时候(如果有的话)我应该在小程序中使用 main 方法?

My understanding was that it is not a good idea to have a main method in an applet because of security issues, is this correct? When (if ever) should I use a main method in an applet?

此外,我的教授正在使用这样的小程序嵌入到 PowerPoint 演示文稿中,然后他可以在演示过程中轻松运行.如果小程序使用 inti() 而不是 main(),那么是否仍然可以运行嵌入到 PowerPoint 演示文稿中的这样的小程序?我这么问是因为我看不出有什么理由他会在这样的小程序中使用 main 方法,除非小程序在嵌入到 PowerPoint 演示文稿中时需要它才能正常运行.

Also, my professor is using applets like this embedded into a PowerPoint presentation which he can then run easily during his presentation. Would it still be possible to run an applet like this which was embedded into a PowerPoint presentation if the applet used inti() instead of main()? I ask that because I cannot see any reason why he would use a main method in such an applet unless it was required for the applet to run properly when embedded into a PowerPoint presentation.

//EventDispatcherThreadDemo.java

import javax.swing.*;

public class EventDispatcherThreadDemo extends Japplet{

    public EventDispatcherThreadDemo(){
        add(new JLabel("Hi, it runs from an event dispatch thread"));
    }

    public static void main(Stirng[] args){

        SwingUtilities.invokeLater(new Runnable(){

            public void run(){
                JFrame frame = new JFrame("EventDispatcherThreadDemo");
                frame.add(new EventDispatcherThreadDemo());
                frame.setSize(200, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });

    }

}

附言这与作业无关,只是想了解更多关于小程序的信息.

P.S. This has nothing to do with homework, just trying to learn more about applets.

推荐答案

main 方法对小程序没有任何特殊意义.

The main method does not have any special meaning to an applet.

您的教授将它与几行代码一起包含在 JFrame 中以启动类,以便也能够使用简单的java foobar"命令从命令行运行它.这与必须运行appletviewer foobar"命令相反,该命令可能会在从其他程序(如 PowerPoint)启动时出现问题.虽然没有给出完整的Applet环境,但是演示程序可能很简单,没关系.

Your professor is including it along with a few lines to fire up the class in a JFrame, to be able to run it from the command line too with a simple "java foobar" command. This as opposed to having to run the "appletviewer foobar" command which may have issues with being launched from another program like PowerPoint. it does not give the full Applet environment though, but the demonstration programs may be so simple, it doesn't matter.

我不知道他为什么选择这样做,而不是仅仅向您展示一个带有小程序的大型 HTML 页面.

Why he chose to do so instead of just presenting you with a big HTML page with the applets in, I do not know.

这篇关于在小程序中使用 main 方法是否合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆