简体   繁体   English

用Java显示小程序

[英]Displaying an Applet in Java

So I have two classes. 所以我有两节课。 One is a class called Main. 一个是称为Main的类。 The Main class is supposed to process some data. Main类应该处理一些数据。

Then I have another class called MainApplet, an Applet of course. 然后我有另一个类叫做MainApplet,当然是一个Applet。 How can I display MainApplet from the Main class? 如何从Main类显示MainApplet? This is what I have so far, but the applet does not show: 到目前为止,这是我所拥有的,但是小程序未显示:

public class Main {
public static void main(String args[]) {
    System.out.println("Starting application.");

    MainApplet Main = new MainApplet();
    Main.setVisible(true);
    Main.show();
} }

How can I display MainApplet from the Main class? 如何从Main类显示MainApplet?

You don't. 你不知道 You display an applet from HTML code. 您从HTML代码显示一个applet。

Are you sure your GUI is in fact an applet ? 您确定您的GUI实际上是一个applet吗? Does it extend JApplet or Applet? 它扩展了JApplet还是Applet? If so and you want to show it on the desktop through code, then don't make it an applet but instead display a JFrame. 如果是这样,并且您希望通过代码在桌面上显示它,那么就不要使其成为applet,而是显示一个JFrame。 The Java Swing tutorials will show you how to do this: How to use Swing Components Java Swing教程将向您展示如何执行此操作: 如何使用Swing组件

Edit 编辑
you state: 您声明:

Basically I have a Main class that is not an applet. 基本上,我有一个不是applet的Main类。 It doesn't extend anything. 它不会扩展任何内容。 Then I have another class named MainApplet that is an applet (extends JApplet). 然后,我有另一个名为MainApplet的类,它是一个applet(扩展了JApplet)。 I want to run Main first, then display MainApplet after... but I can do it the other way around if needed. 我想先运行Main,然后在之后显示MainApplet,但是如果需要的话,我可以采用其他方法。

You don't sound like you're running your code from a web page (for some reason you're still keeping this information from us), so the solution is not to use applets for this. 您听起来不像是从网页上运行代码(出于某种原因您仍在向我们保留此信息),因此解决方案是不使用applet。 Instead create a JFrame. 而是创建一个JFrame。 Please check out the tutorials that I've linked to above since an applet is not appropriate for your needs. 由于小应用程序不适合您的需要,请查看我上面链接的教程。

You will have a main that creates your GUI, that passes any information into the GUI via constructor or method parameters, and then that tells the GUI to show itself (by calling setVisible(true) if it's a JFrame). 您将拥有一个创建GUI的主体,该主体通过构造函数或方法参数将任何信息传递到GUI中,然后告诉GUI显示自身(如果是JFrame,则调用setVisible(true) )。

Your mainApplet class must extend Applet. 您的mainApplet类必须扩展Applet。 You would then replace your main(...) method with init(...) as this is the method that gets called when you open an Applet. 然后,您可以用init(...)替换main(...)方法,因为这是打开Applet时调用的方法。 You can test this in most environments such as Eclipse. 您可以在大多数环境(例如Eclipse)中对此进行测试。 It order to actually put the Applet on a webpage, you must tell the applet to display using HTML in the page. 为了将小程序实际放置在网页上,您必须告诉小程序在页面中使用HTML进行显示。

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

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