Import java. awt .*;
Import java. awt. event .*;
Import java. io .*;
Import javax. swing .*;
Import javax. swing. text .*;
// Simple text editor
Public class EditorDemo extends JFrame {
JTextPane textPane = new JTextPane (); // text pane, editing window
JLabel statusBar = new JLabel (); // Status Bar
JFileChooser filechooser = new JFileChooser (); // file Selector
Public EditorDemo () {// Constructor
Super ("simple text editor"); // calls the parent class Constructor
Action [] actions = // Action array, various operation commands
{
New NewAction (),
New OpenAction (),
New SaveAction (),
New CutAction (),
New CopyAction (),
New PasteAction (),
New AboutAction (),
New ExitAction ()};
SetJMenuBar (createJMenuBar (actions); // set the menu bar
Container container = getContentPane (); // obtain the Container
Container. add (createJToolBar (actions), BorderLayout. NORTH); // add a toolbar
Container. add (textPane, BorderLayout. CENTER); // add a text pane.
Container. add (statusBar, BorderLayout. SOUTH); // add the status bar.
SetSize (330,200); // set the window size
SetVisible (true); // sets the window visibility.
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the window is closed.
}
Private JMenuBar createJMenuBar (Action [] actions) {// create a menu bar
JMenuBar menubar = new JMenuBar (); // instantiate the menu bar
JMenu menuFile = new JMenu ("file"); // instantiate the menu
JMenu menuEdit = new JMenu ("edit ");
JMenu menuAbout = new JMenu ("help ");
MenuFile. add (new JMenuItem (actions [0]); // add a new menu item
MenuFile. add (new JMenuItem (actions [1]);
MenuFile. add (new JMenuItem (actions [2]);
MenuFile. add (new JMenuItem (actions [7]);
MenuEdit. add (new JMenuItem (actions [3]);
MenuEdit. add (new JMenuItem (actions [4]);
MenuEdit. add (new JMenuItem (actions [5]);
MenuAbout. add (new JMenuItem (actions [6]);
Menubar. add (menuFile); // add a menu
Menubar. add (menuEdit );
Menubar. add (menuAbout );
Return menubar; // return to the menu bar
}
Private JToolBar createJToolBar (Action [] actions) {// create a toolbar
JToolBar toolBar = new JToolBar (); // instantiate the toolBar
For (int I = 0; I <actions. length; I ++ ){
JButton bt = new JButton (actions [I]); // click the button to create an instance.
Bt. setRequestFocusEnabled (false); // you do not need to set the focus.
ToolBar. add (bt); // add the button to the toolBar
}
Return toolBar; // return toolBar
}
Class NewAction extends actaction {// create a file command
Public NewAction (){
Super ("new ");
}
Public void actionreceivmed (ActionEvent e ){
TextPane. setDocument (new DefaultStyledDocument (); // clear the document
}
}
Class OpenAction extends actaction {// open a file command
Public OpenAction (){
Super ("open ");
}
Public void actionreceivmed (ActionEvent e ){
Int I = filechooser. showOpenDialog (EditorDemo. this); // The open file dialog box is displayed.
If (I = JFileChooser. APPROVE_OPTION) {// click open option in the dialog box
File f = filechooser. getSelectedFile (); // obtain the selected File
Try {
InputStream is = new FileInputStream (f); // get the file input stream
TextPane. read (is, "d"); // read the file to the text pane
} Catch (Exception ex ){
Ex. printStackTrace (); // output error message
}
}
}
}
Class SaveAction extends AbstractAction {// Save the command
Public SaveAction (){
Super ("save ");
}
Public void actionreceivmed (ActionEvent e ){
Int I = filechooser. showSaveDialog (EditorDemo. this); // The save file dialog box is displayed.
If (I = JFileChooser. APPROVE_OPTION) {// click the Save button in the dialog box.
File f = filechooser. getSelectedFile (); // obtain the selected File
Try {
FileOutputStream out = new FileOutputStream (f); // get the file output stream
Out. write (textPane. getText (). getBytes (); // write the file
} Catch (Exception ex ){
Ex. printStackTrace (); // output error message
}
}
}
}
Class ExitAction extends AbstractAction {// exit command
Public ExitAction (){
Super ("exit ");
}
Public void actionreceivmed (ActionEvent e ){
System. exit (0); // exit the program
}
}
Class CutAction extends define actaction {// cut command
Public CutAction (){
Super ("cut ");
}
Public void actionreceivmed (ActionEvent e ){
TextPane. cut (); // call the cut command of the text pane
}
}
Class CopyAction extends actaction {// Copy command
Public CopyAction (){
Super ("copy ");
}
Public void actionreceivmed (ActionEvent e ){
TextPane. copy (); // call the copy Command of the text pane
}
}
Class PasteAction extends actaction {// paste command
Public PasteAction (){
Super ("Paste ");
}
Public void actionreceivmed (ActionEvent e ){
TextPane. paste (); // call the paste command of the text pane
}
}
Class AboutAction extends actaction {// option command
Public AboutAction (){
Super ("about ");
}
Public void actionreceivmed (ActionEvent e ){
JOptionPane. showMessageDialog (EditorDemo. this, "simple text editor Demo"); // displays Software Information
}
}
Public static void main (String [] args ){
New EditorDemo ();
}
}