问题
I installed the WindowBuilder Plugin for Eclipse 3.7.
I created a very simple GUI with various methods (JFrame, JPanel, Application etc.). I tried to Export my Project as a runnable JAR / a .jar file. I have to choose the Main Class: It does not show anything to select! The Export still works but the file is useless.
How do I export my GUI class properly?
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:
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. 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?
@Andrew: The "Application" meant one of the options in the WindowBuilder->Swing Designer menu. which is in fact called Application Window, and there are also JApplet, JFrame, JDialog and so on. 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...
回答1:
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
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
回答2:
Try this code - works as either applet or free floating (frame based) app.
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;
}
}
来源:https://stackoverflow.com/questions/10826141/java-windowbuilder-export-as-jar-runnable-or-applet