Sie sind auf Seite 1von 4

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 { private TextArea textArea = new TextArea("", 0,0, 008 TextArea.SCROLLBARS_VERTICAL_ONLY); 009 010 011 012 013 014 015 016 017 018 019 window public Notepad() { this.setSize(500, 300); // set the initial size of the window this.setTitle("Java Notepad Tutorial"); // set the title of the private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item private Menu file = new Menu(); // our File menu // what's going in File? let's see... private MenuItem openFile = new MenuItem(); // an open option

private MenuItem saveFile = new MenuItem(); // a save option private MenuItem close = new MenuItem(); // and a close option!

setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed) this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); 020 // set a default font for the TextArea // this is why we didn't have to worry about the size of the TextArea! this.getContentPane().setLayout(new BorderLayout()); // the 022 BorderLayout bit makes it fill it automatically 021 023 024 025 026 027 028 // 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 029 031 032 // 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! 033 035 this.file.setLabel("File"); // add our menu bar into the GUI this.setMenuBar(this.menuBar); this.menuBar.add(this.file); // we'll configure this later this.getContentPane().add(textArea);

036 037 038 039 040 041

// now we can start working on the content of the menu~ this gets a little repetitive, // so please bare with me! // time for the repetitive stuff. let's add the "Open" option this.openFile.setLabel("Open"); // set the label of the menu item

this.openFile.addActionListener(this); // add an action listener (so we know when it's been clicked this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); 042 // set a keyboard shortcut 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 have // the default closer, as stated at the beginning of this tutorial. // this means that we actually have TWO shortcuts to close: // 1) the default close operation (example, Alt+F4 on Windows) // and finally, the close option this.close.setLabel("Close"); // along with our "CTRL+F4" shortcut to close the window, we also // and the save... this.saveFile.setLabel("Save"); this.saveFile.addActionListener(this); this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false)); this.file.add(this.saveFile); this.file.add(this.openFile); // add it to the "File" menu

// 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 060 061 062 063 064 065 public void actionPerformed (ActionEvent e) { // if the source of the event was our "close" option } this.close.addActionListener(this); this.file.add(this.close);

if (e.getSource() == this.close) this.dispose(); // dispose all resources and close the 066 application 067 068 069 // if the source was the "open" option else if (e.getSource() == this.openFile) { JFileChooser open = new JFileChooser(); // open up a file 070 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) // NOTE: because we are OPENing a file, we call 072 showOpenDialog~ 073 074 075 // if the user clicked OK, we have "APPROVE_OPTION" // so we want to open the file

if (option == JFileChooser.APPROVE_OPTION) { this.textArea.setText(""); // clear the TextArea before 076 applying the file contents 077 try { // create a scanner to read the file 078 (getSelectedFile().getPath() will get the path to the file) Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); while (scan.hasNext()) // while there's still 080 something to read 079 this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea 082 } catch (Exception ex) { // catch any exceptions, and... 081 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 chooser int option = save.showSaveDialog(this); // similar to the open file, only this time we call // showSaveDialog instead of showOpenDialog // if the user clicked OK (and not cancel) if (option == JFileChooser.APPROVE_OPTION) { try { // and lastly, if the source of the event was the "save" option else if (e.getSource() == this.saveFile) { JFileChooser save = new JFileChooser(); // again, open a file } } } // ...write to the debug console System.out.println(ex.getMessage());

// create a buffered writer to write to a file BufferedWriter out = new BufferedWriter(new 098 FileWriter(save.getSelectedFile().getPath())); out.write(this.textArea.getText()); // write the contents of the TextArea to the file 100 out.close(); // close the file stream 099 101 102 103 104 } } catch (Exception ex) { // again, catch any exceptions and... // ...write to the debug console System.out.println(ex.getMessage());

105 106 107

} }

} // the main method, for actually creating our notepad and setting it 108 to visible. 109 110 111 112 } 113 } public static void main(String args[]) { Notepad app = new Notepad(); app.setVisible(true);

Das könnte Ihnen auch gefallen