简体   繁体   English

Java WindowBuilder->导出为.jar(Runnable或Applet)

[英]Java WindowBuilder -> Export as .jar (Runnable or Applet)

I installed the WindowBuilder Plugin for Eclipse 3.7. 我安装了Eclipse 3.7的WindowBuilder插件。

I created a very simple GUI with various methods (JFrame, JPanel, Application etc.). 我用各种方法(JFrame,JPanel,Application等)创建了一个非常简单的GUI。 I tried to Export my Project as a runnable JAR / a .jar file. 我试图将我的项目导出为可运行的JAR / .jar文件。 I have to choose the Main Class: It does not show anything to select! 我必须选择Main Class:它没有显示任何内容可供选择! The Export still works but the file is useless. 导出仍然有效,但是文件无用。

How do I export my GUI class properly? 如何正确导出我的GUI类?

EDIT: Seems I need a main method to make the class show up in the main class list. 编辑:似乎我需要一个主要的方法来使该类显示在主类列表中。 But the problem is, that WindowBuilder creates a class like this: 但是问题是,WindowBuilder创建了一个像这样的类:

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class myGuiClass extends JApplet {

    /**
     * Create the applet.
     */
    public myGuiClass() {
        getContentPane().setLayout(null);

        JButton btnMybutton = new JButton("myButton");
        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                System.out.println("Button pressed!");
            }
        });
        btnMybutton.setBounds(10, 11, 91, 23);
        getContentPane().add(btnMybutton);

    }
}

If I create a main method here, I cannot create my JApplet, as it it not allowed to reference its non-static constructor in the static main method. 如果在此处创建主方法,则无法创建JApplet,因为它不允许在静态main方法中引用其非静态构造函数。 At least I dont know how to do it. 至少我不知道该怎么做。 So how would I go about exporting this very simple one class program to a runnable .jar or an applet? 那么,如何将这个非常简单的类程序导出到可运行的.jar或applet中呢?

@Andrew: The "Application" meant one of the options in the WindowBuilder->Swing Designer menu. @Andrew:“应用程序”意味着WindowBuilder-> Swing Designer菜单中的选项之一。 which is in fact called Application Window, and there are also JApplet, JFrame, JDialog and so on. 实际上称为应用程序窗口,还有JApplet,JFrame,JDialog等。 I would like to have something executable that I can bring to another PC or run via web, so I dont have to deal with crude executing things with the cmd window... 我想拥有一些可执行文件,可以带到另一台PC或通过Web运行,因此我不必使用cmd窗口来处理粗暴的执行工作...

Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included 试试这个,这是在eclipse中创建jar或可运行jar的方法,项目中的所有外部库都将包括在内

File -> Export-> Java ->Runnbale JAR file

Launch configuration : your Class containing the public static void main(String[] args)

Export destination : Target place

Library Handling:

Package required libraries into generated JAR

FINISH

Try this code - works as either applet or free floating (frame based) app. 尝试使用此代码-可作为applet或自由浮动(基于框架)的应用程序使用。

import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/** This is an hybrid application/applet.  It can be run
 * as either a JFrame or JApplet. */
public class MyGuiApp extends JApplet {

    /** Construct the GUI for an applet. */
    public void init() {
        try {
            SwingUtilities.invokeAndWait( new Runnable() {
                public void run() {
                    GUI gui = new GUI();
                    getContentPane().add(gui.getGui());
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /** Launch the GUI in a frame. */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override 
            public void run() {
                JFrame f = new JFrame("My GUI");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                GUI gui = new GUI();
                f.getContentPane().add(gui.getGui());
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

class GUI {

    public JComponent getGui() {
        // Use layouts! ..
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING));
        // ..possibly with borders.
        p.setBorder(new EmptyBorder(10,10,10,10));

        JButton btnMybutton = new JButton("myButton");
        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                System.out.println("Button pressed!");
            }
        });
        p.add(btnMybutton);
        return p;
    }
}

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

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