001 | import javax.swing.*; |
002 | import java.awt.*; |
003 | import java.awt.event.*; |
004 | import java.util.Scanner; |
005 | import java.io.*; |
006 |
007 | public class Notepad extends JFrame implements ActionListener { |
008 | private TextArea textArea = new TextArea( "" , 0 , 0 , TextArea.SCROLLBARS_VERTICAL_ONLY); |
009 | private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item |
010 | private Menu file = new Menu(); // our File menu |
011 | // what's going in File? let's see... |
012 | private MenuItem openFile = new MenuItem(); // an open option |
013 | private MenuItem saveFile = new MenuItem(); // a save option |
014 | private MenuItem close = new MenuItem(); // and a close option! |
015 | |
016 | public Notepad() { |
017 | this .setSize( 500 , 300 ); // set the initial size of the window |
018 | this .setTitle( "Java Notepad Tutorial" ); // set the title of the window |
019 | setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed) |
020 | this .textArea.setFont( new Font( "Century Gothic" , Font.BOLD, 12 )); // set a default font for the TextArea |
021 | // this is why we didn't have to worry about the size of the TextArea! |
022 | this .getContentPane().setLayout( new BorderLayout()); // the BorderLayout bit makes it fill it automatically |
023 | this .getContentPane().add(textArea); |
024 |
025 | // add our menu bar into the GUI |
026 | this .setMenuBar( this .menuBar); |
027 | this .menuBar.add( this .file); // we'll configure this later |
028 | |
029 | // first off, the design of the menuBar itself. Pretty simple, all we need to do |
030 | // is add a couple of menus, which will be populated later on |
031 | this .file.setLabel( "File" ); |
032 | |
033 | // now it's time to work with the menu. I'm only going to add a basic File menu |
034 | // but you could add more! |
035 | |
036 | // now we can start working on the content of the menu~ this gets a little repetitive, |
037 | // so please bare with me! |
038 | |
039 | // time for the repetitive stuff. let's add the "Open" option |
040 | this .openFile.setLabel( "Open" ); // set the label of the menu item |
041 | this .openFile.addActionListener( this ); // add an action listener (so we know when it's been clicked |
042 | this .openFile.setShortcut( new MenuShortcut(KeyEvent.VK_O, false )); // set a keyboard shortcut |
043 | this .file.add( this .openFile); // add it to the "File" menu |
044 | |
045 | // and the save... |
046 | this .saveFile.setLabel( "Save" ); |
047 | this .saveFile.addActionListener( this ); |
048 | this .saveFile.setShortcut( new MenuShortcut(KeyEvent.VK_S, false )); |
049 | this .file.add( this .saveFile); |
050 | |
051 | // and finally, the close option |
052 | this .close.setLabel( "Close" ); |
053 | // along with our "CTRL+F4" shortcut to close the window, we also have |
054 | // the default closer, as stated at the beginning of this tutorial. |
055 | // this means that we actually have TWO shortcuts to close: |
056 | // 1) the default close operation (example, Alt+F4 on Windows) |
057 | // 2) CTRL+F4, which we are about to define now: (this one will appear in the label) |
058 | this .close.setShortcut( new MenuShortcut(KeyEvent.VK_F4, false )); |
059 | this .close.addActionListener( this ); |
060 | this .file.add( this .close); |
061 | } |
062 | |
063 | public void actionPerformed (ActionEvent e) { |
064 | // if the source of the event was our "close" option |
065 | if (e.getSource() == this .close) |
066 | this .dispose(); // dispose all resources and close the application |
067 | |
068 | // if the source was the "open" option |
069 | else if (e.getSource() == this .openFile) { |
070 | JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open) |
071 | int option = open.showOpenDialog( this ); // get the option that the user selected (approve or cancel) |
072 | // NOTE: because we are OPENing a file, we call showOpenDialog~ |
073 | // if the user clicked OK, we have "APPROVE_OPTION" |
074 | // so we want to open the file |
075 | if (option == JFileChooser.APPROVE_OPTION) { |
076 | this .textArea.setText( "" ); // clear the TextArea before applying the file contents |
077 | try { |
078 | // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file) |
079 | Scanner scan = new Scanner( new FileReader(open.getSelectedFile().getPath())); |
080 | while (scan.hasNext()) // while there's still something to read |
081 | this .textArea.append(scan.nextLine() + "\n" ); // append the line to the TextArea |
082 | } catch (Exception ex) { // catch any exceptions, and... |
083 | // ...write to the debug console |
084 | System.out.println(ex.getMessage()); |
085 | } |
086 | } |
087 | } |
088 | |
089 | // and lastly, if the source of the event was the "save" option |
090 | else if (e.getSource() == this .saveFile) { |
091 | JFileChooser save = new JFileChooser(); // again, open a file chooser |
092 | int option = save.showSaveDialog( this ); // similar to the open file, only this time we call |
093 | // showSaveDialog instead of showOpenDialog |
094 | // if the user clicked OK (and not cancel) |
095 | if (option == JFileChooser.APPROVE_OPTION) { |
096 | try { |
097 | // create a buffered writer to write to a file |
098 | BufferedWriter out = new BufferedWriter( new FileWriter(save.getSelectedFile().getPath())); |
099 | out.write( this .textArea.getText()); // write the contents of the TextArea to the file |
100 | out.close(); // close the file stream |
101 | } catch (Exception ex) { // again, catch any exceptions and... |
102 | // ...write to the debug console |
103 | System.out.println(ex.getMessage()); |
104 | } |
105 | } |
106 | } |
107 | } |
108 | // the main method, for actually creating our notepad and setting it to visible. |
109 | public static void main(String args[]) { |
110 | Notepad app = new Notepad(); |
111 | app.setVisible( true ); |
112 | } |
113 | } |
No comments:
Post a Comment