简体   繁体   English

如何让applet接受用户特定的输入?

[英]How to let applet take user specific input?

I have a question on the java applet.I've created a java applet,which is a board game,that can have a 2*2 array with row number and column number both set to 9 by default. 我对Java小程序有一个疑问。我创建了一个Java小程序,它是一个棋盘游戏,可以有一个2 * 2数组,行号和列号都默认设置为9。

Now I want to extend my applet a bit,that the user can specify the size they want on the command-line,then the applet class will create an applet with correspoding size. 现在,我想稍微扩展一下applet,以便用户可以在命令行上指定所需的大小,然后applet类将创建一个具有相应大小的applet。

I try to add a constructor in the applet class,but the Eclipse complains,I also tried another class,which will create an instance of this applet with size as an instance variable,but it is not working. 我尝试在applet类中添加一个构造函数,但是Eclipse抱怨,我还尝试了另一个类,该类将使用大小作为实例变量创建此applet的实例,但是它不起作用。

Could anyone help me a little bit on where to put a main() method that can take care of user-specified board sized,then create an array in my applet class accordingly? 谁能帮我在哪里可以放置一个main()方法,该方法可以照顾用户指定的板子尺寸,然后在我的applet类中相应地创建一个数组?

Thanks a lot. 非常感谢。

Rob

You shouldn't be using a main() method: that's a Java application entry point. 您不应该使用main()方法:这是Java 应用程序的入口点。 Since you already have a Java applet , just work on it so that it asks users for board size etc, before continuing on with what you already have. 由于您已经有了Java applet ,因此只需对其进行处理,以便在继续使用现有的功能之前先询问用户主板的尺寸等。

See also 也可以看看

The main() won't be executed when the applet runs. 小程序运行时不会执行main() Only the Applet#init() will be run. Applet#init()将运行。 Just pop a Swing JOptionPane of type JOptionPane.QUESTION_MESSAGE which asks for user input. 只需弹出类型为JOptionPane.QUESTION_MESSAGE的Swing JOptionPane ,即可请求用户输入。

public void init() {
    String answer = JOptionPane.showInputDialog(null, "Your question here", "Dialog title here", JOptionPane.QUESTION_MESSAGE);
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM