刚学java不久就被其简单所吸引,java里用一个简单的frame类就可以实现窗口和彩单,而用API则不敢想象,至少也要100句,且复杂异常,但java的class文件每次都要编译才能运行,很是不便,而且还要装jdk才行,我摸索了很久,才发现java有以下三种方法实现双击执行:
1) 用集成的开发工具,如:visual j++,jBuilder之类的开发工具,但不是原汁原味的java语言,还有一写自己定义的类库,且用法和jdk不一样,也就不详细说明了。
2) 用jdk自带的jar做成*.jar包,许多书上也有介绍,但都不怎么详细,至少我看了许多本书,没有一本很完整的告诉怎么做jar包,都是自己摸索出来的,具体的方法如下:
⒈如果装了winrar压缩工具一定要在文件关联的选项中去掉jar,不然就以winrar打开。
⒉用以下的程序试一下,注意:一定要有main方法,且不能是小应用程序和dos程序。
用下面的例子试一下,这是jdk1.3.1里带的一个例子。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* An application that displays a JButton and several JRadioButtons.
* The JRadioButtons determine the look and feel used by the application.
*/
public class SimpleExample extends JPanel {
static JFrame frame;
static String metal= "Metal";
static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
static String motif = "Motif";
static String motifClassName =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
static String windows = "Windows";
static String windowsClassName =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
JRadioButton metalButton, motifButton, windowsButton;
public SimpleExample() {
// Create the buttons.
JButton button = new JButton("Hello, world");
button.setMnemonic('h'); //for looks only; button does nada
metalButton = new JRadioButton(metal);
metalButton.setMnemonic('o');
metalButton.setActionCommand(metalClassName);
motifButton = new JRadioButton(motif);
motifButton.setMnemonic('m');
motifButton.setActionCommand(motifClassName);
windowsButton = new JRadioButton(windows);
windowsButton.setMnemonic('w');
windowsButton.setActionCommand(windowsClassName);
// Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(metalButton);
group.add(motifButton);
group.add(windowsButton);
// Register a listener for the radio buttons.
RadioListener myListener = new RadioListener();
metalButton.addActionListener(myListener);
motifButton.addActionListener(myListener);
windowsButton.addActionListener(myListener);
add(button);
add(metalButton);
add(motifButton);
add(windowsButton);
}
/** An ActionListener that listens to the radio buttons. */
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String lnfName = e.getActionCommand();
try {
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
}
catch (Exception exc) {
JRadioButton button = (JRadioButton)e.getSource();
button.setEnabled(false);
updateState();
System.err.println("Could not load LookAndFeel: " + lnfName);
}
}
}
public void updateState() {
String lnfName = UIManager.getLookAndFeel().getClass().getName();
if (lnfName.indexOf(metal) >= 0) {
metalButton.setSelected(true);
} else if (lnfName.indexOf(windows) >= 0) {
windowsButton.setSelected(true);
} else if (lnfName.indexOf(motif) >= 0) {
motifButton.setSelected(true);
} else {
System.err.println("SimpleExample is using an unknown L&F: " + lnfName);
}
}
public static void main(String s[]) {
/*
NOTE: By default, the look and feel will be set to the
Cross Platform Look and Feel (which is currently Metal).
The user may someday be able to override the default
via a system property. If you as the developer want to
be sure that a particular L&F is set, you can do so
by calling UIManager.setLookAndFeel(). For example, the
first code snippet below forcibly sets the UI to be the
System Look and Feel. The second code snippet forcibly
sets the look and feel to the Cross Platform L&F.
Snippet 1:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
Snippet 2:
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
*/
SimpleExample panel = new SimpleExample();
frame = new JFrame("SimpleExample");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
panel.updateState();
}
}
以起主类名:SimpleExample为文件名,假设存在G:\Myprogramme\JAVA SDK下,先在dos下切换到这个目录下,运行javac SimpleExample.java,无误,生成SimpleExample.class后,运行java SimpleExample可以看到结果,是一个可以改变风格的对话框。做jar包之前要做一个 MF文件,打开记事本,其格式如下:
Manifest-Version: 1.0
Main-Class: SimpleExample
Created-By: 1.3.1 (Sun Microsystems Inc.)
只需要改一下类名就可以了,其他的照抄就行。改为MANIFEST.mf存到前面的那个目录下(文件名可以任意取)。Dos下输入jar.exe cvfm Myjar.jar MANIFEST.MF ./ 回车之后,会出现下面的文字:
新增清单(manifest)
新增:SimpleExample.java(读入= 5712) (写出= 2293)(压缩 59%)
新增:MANIFEST.MF(读入= 95) (写出= 92)(压缩 3%)
新增:SimpleExample$1.class(读入= 387) (写出= 272)(压缩 29%)
新增:SimpleExample$RadioListener.class(读入= 1416) (写出= 826)(压缩 41%)
新增:SimpleExample.class(读入= 3032) (写出= 1584)(压缩 47%)
表示包创建成功,在当前目录下就会多一个名为Myjar.jar的文件,双击就可以运行了。
在win98和winxp里运行通过。
3)用windows自带的工具,在系统盘的windows目录下有一个名为jview.exe或vjview.exe的程序,直接把生成的class文件的打开方式改成它就行,然后就可以双击其运行了,不过不是主类的class文件是不能直接双击运行的,此方法只在windows98里成功过,winxp里不行。
欢迎大家与我讨论有关java和MFC的话题,我的EMAIL:ww8375@sina.com