【问题标题】:Export runnable applet IntelliJ IDEA导出可运行小程序 IntelliJ IDEA
【发布时间】:2014-04-08 08:58:09
【问题描述】:

我有一个想要导出为独立可运行 jar 文件的小程序。这将如何在 IntelliJ IDEA 中实现?

我尝试从终端和桌面启动它,它似乎打开但几乎立即退出。使用在 IDE 中运行,小程序可以正常工作。

【问题讨论】:

标签: java intellij-idea applet


【解决方案1】:

Applet 是 javax.swing.Component 的子类,因此您可以像任何其他组件一样将其添加到 JFrame。

因此,您需要做的就是创建一个 main() 方法,该方法创建一个 JFrame 并将小程序添加到其中。 这是一个例子:

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");
      }
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 2016-10-28
    • 2011-12-24
    • 2023-03-24
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多