1、命令:
D:\desktop>javac -d ./ JNotePad.java
D:\desktop>jar cvf jnotepad.jar desktop
added manifest
adding: desktop/(in = 0) (out= 0)(stored 0%)
adding: desktop/11/(in = 0) (out= 0)(stored 0%)
adding: desktop/JNotePad$1.class(in = 520) (out= 342)(deflated 34%)
打开jnotepad.jar\META-INF\MAINIFEST.MF添加:
Main-Class: desktop.JNotePad
2、记事本代码:
package desktop;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
/**
* @author timeriver.wang
* @date 2013-01-04 6:23:45 PM
*/
public class JNotePad extends JFrame {
private JMenu fileMenu;//should be filed or local variables?
private JMenuItem openMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem saveAsMenuItem;
private JMenuItem closeMenuItem;
private JMenuItem aboutMenuItem;
private JTextArea textArea;
private JFileChooser fileChooser;
private boolean isChanged = false;
public JNotePad() {
super( "new txt file" );
setUI();
setEventListener();
setVisible( true );
}
public static void main( String[] args ) {
new JNotePad();
}
private void setUI() {
setSize( 640, 480 );
JMenuBar menuBar = new JMenuBar();
//-------------------------------//
//file Menu
//-------------------------------//
fileMenu = new JMenu( "file" );
openMenuItem = new JMenuItem( "open" );
// shortcut key,VK_O-->'O',CTRL_MASK-->"ctrl"
openMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_O, InputEvent.CTRL_MASK ) );
saveMenuItem = new JMenuItem( "save" );
saveMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_MASK ) );
saveAsMenuItem = new JMenuItem( "save as" );
closeMenuItem = new JMenuItem( "close" );
closeMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_Q, InputEvent.CTRL_MASK ) );
fileMenu.add( openMenuItem );
fileMenu.addSeparator();
fileMenu.add( saveMenuItem );
fileMenu.addSeparator();
fileMenu.add( saveAsMenuItem );
fileMenu.addSeparator();
fileMenu.add( closeMenuItem );
//-------------------------------//
//help Menu
//-------------------------------//
JMenu helpMenu = new JMenu( "help" );
aboutMenuItem = new JMenuItem( "about" );
helpMenu.add( aboutMenuItem );
//
menuBar.add( fileMenu );
menuBar.add( helpMenu );
//
setJMenuBar( menuBar );
// textArea
textArea = new JTextArea();
textArea.setLineWrap( true );
JScrollPane panel = new JScrollPane( textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
Container contentPane = getContentPane();
contentPane.add( panel, BorderLayout.CENTER );
//
fileChooser = new JFileChooser();
}
private void setEventListener() {
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
closeFile();
}
} );
openMenuItem.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
openFile();
}
} );
saveMenuItem.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
saveFile();
}
} );
saveAsMenuItem.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
saveFileAs();
}
} );
closeMenuItem.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
closeFile();
}
} );
aboutMenuItem.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
JOptionPane.showOptionDialog( null,
"java desktop demo,jar,started by bat command", "about JNotePad",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null,
null );
}
} );
// key event
textArea.addKeyListener( new KeyAdapter() {
public void keyTyped( KeyEvent e ) {
}
} );
// mouse event
textArea.addMouseListener( new MouseAdapter() {
public void mouseReleased( MouseEvent e ) {
}
public void mouseClicked( MouseEvent e ) {
}
} );
}
private void openFile() {
if ( !isChanged ) {
open();
}else {
int option = JOptionPane.showConfirmDialog( null,
"file is changed, do you want to save?", "save file?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null );
if(option == JOptionPane.YES_OPTION){
saveFile();
}else{
open();
}
}
}
private void open() {
int option = fileChooser.showDialog( null, null );
if ( option == JFileChooser.APPROVE_OPTION ) {
try {
BufferedReader buf = new BufferedReader( new FileReader( fileChooser.getSelectedFile() ) );
setTitle( fileChooser.getSelectedFile().toString() );
textArea.setText( "" );
isChanged = true;
String lineSeparator = System.getProperty( "line.separator" );
String text;
while ( ( text = buf.readLine() ) != null ) {
textArea.append( text );
textArea.append( lineSeparator );
}
buf.close();
}
catch ( IOException e ) {
JOptionPane.showMessageDialog( null, e.toString(),
"open file fail!", JOptionPane.ERROR_MESSAGE );
}
}
}
private void saveFile() {
File file = new File( getTitle() );
if ( !file.exists() ) {
saveFileAs();
}else {
try {
BufferedWriter buf = new BufferedWriter( new FileWriter( file ) );
buf.write( textArea.getText() );
buf.close();
isChanged = false;
}
catch ( IOException e ) {
JOptionPane.showMessageDialog( null, e.toString(),
"write file fail!", JOptionPane.ERROR_MESSAGE );
}
}
}
private void saveFileAs() {
int option = fileChooser.showSaveDialog( null );
if ( option == JFileChooser.APPROVE_OPTION ) {
File file = fileChooser.getSelectedFile();
setTitle( file.toString() );
try {
file.createNewFile();
saveFile();
}
catch ( IOException e ) {
JOptionPane.showMessageDialog( null, e.toString(),
"save as file fail!", JOptionPane.ERROR_MESSAGE );
}
}
}
private void closeFile() {
if ( isChanged ) {
dispose();
}else {
int option = JOptionPane.showConfirmDialog( null,
"file has changed,save?", "save file?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null );
if(option == JOptionPane.YES_OPTION){
saveFile();
}else{
dispose();
}
}
}
}