简体   繁体   中英

How can I run applet code after calling MainFrame?

How can I run some code after calling MainFrame with Java Applets? Is this possible?

Frame frame = new MainFrame(new ClassName(),256,256);
System.out.println("This won't print!");

Can someone explain how I can get that to print after calling MainFrame?

Java does not have a MainFrame class in it's standard libraries.

What you are probably seeing is a program that can run as both an Applet and an application.
When running as an application, the main GUI class is apparently called MainFrame , and the line you quote is where it is constructed. Note that it receives an instance of class ClassName ; that's probably where the logic of the program resides (the model).

Your System.out.println will be called after the MainFrame(...) constructor is done.
Only if the system is exited (by a call to System.exit(int) for example) before it can return from the constructor call, will your System.out.println not be reached.

As a simple example, take a look at this code:

package mainframe;

import javax.swing.JFrame;

/**
 *
 * @author codeguru <codeguru@users.sourceforge.net>
 */
public class MainFrame extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new MainFrame();

        System.out.println("This prints.");
    }
}

This gives the expected output:

This prints.

From your original code, I don't see where the applet is that you refer to in your question's title. In order to help you, we need to know more about MainClass and ClassName . These look like custom classes which you wrote or are part of the example you are studying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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