简体   繁体   中英

Export runnable applet IntelliJ IDEA

I have an applet I would like to export as a standalone runnable jar file. How would this be achieved in IntelliJ IDEA?

I have tried launching it from terminal and from desktop and it seems to open but quits almost straight away. The applet works fine using run in the IDE.

Applet is a sub-class of javax.swing.Component, so you can add it to a JFrame like any other component.

So, all you need to do is create a main() method that creates a JFrame and adds the applet to it. Here's an example:

import javax.swing.*;
import java.awt.*;

public class HelloWorldApplet extends JApplet {
   public static void main(String[] args) {

     // create and set up the applet
     HelloWorldApplet applet = new HelloWorldApplet();
     applet.setPreferredSize(new Dimension(500, 500));
     applet.init();

     // create a frame to host the applet, which is just another type of Swing Component        
     JFrame mainFrame = new JFrame();               
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     // add the applet to the frame and show it
     mainFrame.getContentPane().add(applet);
     mainFrame.pack();      
     mainFrame.setVisible(true);

     // start the applet
     applet.start();
   }

   public void init() {
     try {
        SwingUtilities.invokeAndWait(new Runnable() {
           public void run() {
              JLabel label = new JLabel("Hello World");
              label.setHorizontalAlignment(SwingConstants.CENTER);
              add(label);
           }
         });
      } catch (Exception e) {
           System.err.println("createGUI didn't complete successfully");
      }
    }
}

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