|
Demo 2: Generate a Windows GUI Application
Function: Convert JAR archive into Executive.
Part 1: Build a java program and create a jar charchive
1. Compose a java program D:\test\HelloWorld.java:
package test;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class HelloWorld extends JFrame
{
public HelloWorld()
{
JTextField text = new JTextField("Hello, world");
this.getContentPane().add(text);
}
public static void main(String argv[])
{
HelloWorld win = new HelloWorld();
Toolkit tk = Toolkit.getDefaultToolkit();
int Width = tk.getScreenSize().width;
int Height = tk.getScreenSize().height;
// size and pos
win.setSize(100, 100);
win.setLocation((Width - 100) / 2, (Height - 100) / 2);
// show
win.setVisible(true);
win.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
} |
2. Compile, we get HelloWorld.class:
D:\>javac -target 1.2 test/HelloWorld.java |
3. Use jar.exe to create test2.jar:
D:\>jar cvf test2.jar test/HelloWorld.class |
Part 2: Use free tool to create .exe file
1. Download and install this free tool:
[Free Edition Download]
2. Launch J2EWizard, select the jar file, then press next to continue:
3. Select the application type. Here we select "Windows GUI Application":
4. Input the main class to start:
5. Type in the exe filename you want to get:
6. Press next, the exe file is generated.
Part 3: Run the Exe file
1. Double click or type on console: test2, the result:
2. Download the program and result exe in this demo:
[test.zip] - 20kb
More Demos:
Demo 1: How to generate a CONSOLE application in java?
Demo 3: How to build and create a Window NT Service in java?
Demo 4: Window NT Service supports PAUSE/CONTINUE.
Demo 5: System Taskbar Tray Icon and Event Log
|
|
|