Post: [Java] Notepad Template
02-02-2012, 01:15 AM #1
tylerallmighty
Human After All
(adsbygoogle = window.adsbygoogle || []).push({}); Here's a neat little Notepad Template (not written by me). This version has the comments removed and a few variables added in to simplify it.

    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("New File... Happy", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();


private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();

public static final int WIDTH = 700;
public static final int HEIGHT = 400;
public static final String TITLE = "Notepad Template";


public Notepad() {
this.setSize(WIDTH, HEIGHT);
this.setTitle(TITLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(textArea);

this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);


this.file.setLabel("File");


this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));

this.file.add(this.openFile);


this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);


this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}


public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.close)
this.dispose();


else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText("");
try {
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext())
this.textArea.append(scan.nextLine() + "\n");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}


else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText());
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}


public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}


This next one is the from the original post completely unedited with comments left in. Smile

    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
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!

public Notepad() {
this.setSize(500, 300); // set the initial size of the window
this.setTitle("Java Notepad Tutorial"); // set the title of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed)
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // 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 BorderLayout bit makes it fill it automatically
this.getContentPane().add(textArea);


// add our menu bar into the GUI
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file); // we'll configure this later

// first off, the design of the menuBar itself. Pretty simple, all we need to do
// is add a couple of menus, which will be populated later on
this.file.setLabel("File");

// now it's time to work with the menu. I'm only going to add a basic File menu
// but you could add more!

// 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)); // set a keyboard shortcut
this.file.add(this.openFile); // add it to the "File" menu

// 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);

// and finally, the close option
this.close.setLabel("Close");
// along with our "CTRL+F4" shortcut to close the window, we also 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)
// 2) CTRL+F4, which we are about to define now: (this one will appear in the label)
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}

public void actionPerformed (ActionEvent e) {
// if the source of the event was our "close" option
if (e.getSource() == this.close)
this.dispose(); // dispose all resources and close the application

// if the source was the "open" option
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
// NOTE: because we are OPENing a file, we call showOpenDialog~
// 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 applying the file contents
try {
// create a scanner to read the file (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 something to read
this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
} catch (Exception ex) { // catch any exceptions, and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}

// 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 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 {
// create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the TextArea to the file
out.close(); // close the file stream
} catch (Exception ex) { // again, catch any exceptions and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
}
// the main method, for actually creating our notepad and setting it to visible.
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}


Last edited by tylerallmighty ; 02-03-2012 at 03:44 PM.
02-02-2012, 05:00 PM #2
Renegdr
Do a barrel roll!
Could you post a version that includes the comments please?
02-02-2012, 07:01 PM #3
Epic?
Awe-Inspiring
Originally posted by Renegdr View Post
Could you post a version that includes the comments please?


Usually it wouldn't make sense to remove comments, but if !bananaman! removed the comments, it's possible that something about the comments were off or wrong. As such I added accurate comments for you. They're not original, but they should do a good job of explaining the source code anyways:
    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class Notepad extends JFrame implements ActionListener {
// declare the text/writing area
private TextArea textArea = new TextArea("New File... Happy", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);

// declare the menubar and the "File" option to go on the menu bar
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();

// declare items of the "File" option of the menu bar
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();

// constants to store various properties/settings of the window
public static final int WIDTH = 700;
public static final int HEIGHT = 400;
public static final String TITLE = "Notepad Template";

// Constructor
public Notepad() {
// window settings
this.setSize(WIDTH, HEIGHT); // 700 by 400
this.setTitle(TITLE); // title of the window is "Notepad Template"
setDefaultCloseOperation(EXIT_ON_CLOSE); // closes/exits the application when the user tells it to

this.setLocationRelativeTo(null); // center window in screen

// settings for the font in the text area
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));

// sets the content pane of the JFrame to a border layout
// allows for division of the panel into North, South, East, West and Center
this.getContentPane().setLayout(new BorderLayout());

// add the text area to the window
this.getContentPane().add(textArea);

// set the menubar of the window to the declared menubar
this.setMenuBar(this.menuBar);

// add the file menu to the menu bar
this.menuBar.add(this.file);

// set the label for the file menu as "File"
this.file.setLabel("File");

// add the Open option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
this.file.add(this.openFile);


/// add the Save option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);


// add the Close option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}


/** Handles all events and button clicks */
public void actionPerformed(ActionEvent e) {
// if the Close option was selected - determines if the source of the event was the Close option in the File menu
if (e.getSource() == this.close)
this.dispose(); // destroy all componenets, release all screen resources, and return consumed memory to OS

// if the Open option was selected
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // declare a new file-open/selection dialog
int option = open.showOpenDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the user approved the selected file
this.textArea.setText(""); // clear the text area
try {
// use a scanner and filereader to read in the file
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // as long as there is something left in the file
this.textArea.append(scan.nextLine() + "\n"); // add the next line of the file to the text area
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exceptions
}
}
}

// if the Save option was selected
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // declare a new file-saving dialog
int option = save.showSaveDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the result is approval (the user approved the name and path)
try {
// use a bufferedwriter and filewriter to write to the file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the text area to the file
out.close(); // close the stream
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exception
}
}
}
}

/* Program Starts Here */
public static void main(String args[]) {
Notepad app = new Notepad(); // create a new notepad application
app.setVisible(true); // set it visible, thus opening the application
}
}


Simply put, the main method is contained within the Notepad class. The Notepad class inherits from JFrame (part of the Swing GUI toolkit, it's effectively a basic window) and implements ActionListener (which gives you access to action/event related methods).

The constructor for the Notepad class contains all of the basic settings and preparations for the JFrame, as well as all of its components (it also adds the components to the content panel of the JFrame). The constructor is obviously called when the Notepad object is constructed in the main method.

The actionPerformed(ActionEvent) method is from ActionListener. It is called whenever an action or event (such as the clicking of a button) occurs to any of the components it was applied to (which in this case would be the Open, Save, and Close options of the File menu in the menubar). The body of the method is used to handle these events.

EDIT: I thought it would interest you that while a BorderLayout is applied to the JFrame (and I even mention it in the comments of the source code), it actually doesn't do anything as of the current time (as no components are ever positioned according to it).
Last edited by Epic? ; 02-02-2012 at 07:07 PM.

The following user thanked Epic? for this useful post:

Renegdr
02-02-2012, 10:17 PM #4
Renegdr
Do a barrel roll!
Originally posted by Epic
Usually it wouldn't make sense to remove comments, but if !bananaman! removed the comments, it's possible that something about the comments were off or wrong. As such I added accurate comments for you. They're not original, but they should do a good job of explaining the source code anyways:
    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class Notepad extends JFrame implements ActionListener {
// declare the text/writing area
private TextArea textArea = new TextArea("New File... Happy", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);

// declare the menubar and the "File" option to go on the menu bar
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();

// declare items of the "File" option of the menu bar
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();

// constants to store various properties/settings of the window
public static final int WIDTH = 700;
public static final int HEIGHT = 400;
public static final String TITLE = "Notepad Template";

// Constructor
public Notepad() {
// window settings
this.setSize(WIDTH, HEIGHT); // 700 by 400
this.setTitle(TITLE); // title of the window is "Notepad Template"
setDefaultCloseOperation(EXIT_ON_CLOSE); // closes/exits the application when the user tells it to

this.setLocationRelativeTo(null); // center window in screen

// settings for the font in the text area
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));

// sets the content pane of the JFrame to a border layout
// allows for division of the panel into North, South, East, West and Center
this.getContentPane().setLayout(new BorderLayout());

// add the text area to the window
this.getContentPane().add(textArea);

// set the menubar of the window to the declared menubar
this.setMenuBar(this.menuBar);

// add the file menu to the menu bar
this.menuBar.add(this.file);

// set the label for the file menu as "File"
this.file.setLabel("File");

// add the Open option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
this.file.add(this.openFile);


/// add the Save option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);


// add the Close option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}


/** Handles all events and button clicks */
public void actionPerformed(ActionEvent e) {
// if the Close option was selected - determines if the source of the event was the Close option in the File menu
if (e.getSource() == this.close)
this.dispose(); // destroy all componenets, release all screen resources, and return consumed memory to OS

// if the Open option was selected
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // declare a new file-open/selection dialog
int option = open.showOpenDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the user approved the selected file
this.textArea.setText(""); // clear the text area
try {
// use a scanner and filereader to read in the file
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // as long as there is something left in the file
this.textArea.append(scan.nextLine() + "\n"); // add the next line of the file to the text area
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exceptions
}
}
}

// if the Save option was selected
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // declare a new file-saving dialog
int option = save.showSaveDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the result is approval (the user approved the name and path)
try {
// use a bufferedwriter and filewriter to write to the file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the text area to the file
out.close(); // close the stream
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exception
}
}
}
}

/* Program Starts Here */
public static void main(String args[]) {
Notepad app = new Notepad(); // create a new notepad application
app.setVisible(true); // set it visible, thus opening the application
}
}


Simply put, the main method is contained within the Notepad class. The Notepad class inherits from JFrame (part of the Swing GUI toolkit, it's effectively a basic window) and implements ActionListener (which gives you access to action/event related methods).

The constructor for the Notepad class contains all of the basic settings and preparations for the JFrame, as well as all of its components (it also adds the components to the content panel of the JFrame). The constructor is obviously called when the Notepad object is constructed in the main method.

The actionPerformed(ActionEvent) method is from ActionListener. It is called whenever an action or event (such as the clicking of a button) occurs to any of the components it was applied to (which in this case would be the Open, Save, and Close options of the File menu in the menubar). The body of the method is used to handle these events.

EDIT: I thought it would interest you that while a BorderLayout is applied to the JFrame (and I even mention it in the comments of the source code), it actually doesn't do anything as of the current time (as no components are ever positioned according to it).


THanks for that Smile I decided to mess about with the source and try to learn some stuff, ended up finding out how to add new menu items but have absolutely no idea how to make them work properly (such as copy, paste, cut etc.) I looked at the api on oracle's website and tried to find the right syntax for it but I was defeated :( Don't suppose I could trouble you to point me in the right direction to find out how to add these functions?
02-03-2012, 04:14 AM #5
Epic?
Awe-Inspiring
Originally posted by Renegdr View Post
THanks for that Smile I decided to mess about with the source and try to learn some stuff, ended up finding out how to add new menu items but have absolutely no idea how to make them work properly (such as copy, paste, cut etc.) I looked at the api on oracle's website and tried to find the right syntax for it but I was defeated :( Don't suppose I could trouble you to point me in the right direction to find out how to add these functions?


Here's a start: You must login or register to view this content., You must login or register to view this content.

If you have any questions, let me know!

The following user thanked Epic? for this useful post:

Renegdr
02-03-2012, 03:47 PM #6
tylerallmighty
Human After All
Originally posted by Renegdr View Post
Could you post a version that includes the comments please?

Done. :y:
Originally posted by Epic
Usually it wouldn't make sense to remove comments, but if !bananaman! removed the comments, it's possible that something about the comments were off or wrong. As such I added accurate comments for you. They're not original, but they should do a good job of explaining the source code anyways:
    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;


public class Notepad extends JFrame implements ActionListener {
// declare the text/writing area
private TextArea textArea = new TextArea("New File... Happy", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);

// declare the menubar and the "File" option to go on the menu bar
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();

// declare items of the "File" option of the menu bar
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();

// constants to store various properties/settings of the window
public static final int WIDTH = 700;
public static final int HEIGHT = 400;
public static final String TITLE = "Notepad Template";

// Constructor
public Notepad() {
// window settings
this.setSize(WIDTH, HEIGHT); // 700 by 400
this.setTitle(TITLE); // title of the window is "Notepad Template"
setDefaultCloseOperation(EXIT_ON_CLOSE); // closes/exits the application when the user tells it to

this.setLocationRelativeTo(null); // center window in screen

// settings for the font in the text area
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));

// sets the content pane of the JFrame to a border layout
// allows for division of the panel into North, South, East, West and Center
this.getContentPane().setLayout(new BorderLayout());

// add the text area to the window
this.getContentPane().add(textArea);

// set the menubar of the window to the declared menubar
this.setMenuBar(this.menuBar);

// add the file menu to the menu bar
this.menuBar.add(this.file);

// set the label for the file menu as "File"
this.file.setLabel("File");

// add the Open option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
this.file.add(this.openFile);


/// add the Save option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);


// add the Close option, add an action listener (to handle clicks) and set a shortcut/hotkey for it
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}


/** Handles all events and button clicks */
public void actionPerformed(ActionEvent e) {
// if the Close option was selected - determines if the source of the event was the Close option in the File menu
if (e.getSource() == this.close)
this.dispose(); // destroy all componenets, release all screen resources, and return consumed memory to OS

// if the Open option was selected
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // declare a new file-open/selection dialog
int option = open.showOpenDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the user approved the selected file
this.textArea.setText(""); // clear the text area
try {
// use a scanner and filereader to read in the file
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // as long as there is something left in the file
this.textArea.append(scan.nextLine() + "\n"); // add the next line of the file to the text area
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exceptions
}
}
}

// if the Save option was selected
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // declare a new file-saving dialog
int option = save.showSaveDialog(this); // show the dialog and record the results
if (option == JFileChooser.APPROVE_OPTION) { // if the result is approval (the user approved the name and path)
try {
// use a bufferedwriter and filewriter to write to the file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the text area to the file
out.close(); // close the stream
} catch (Exception ex) {
System.out.println(ex.getMessage()); // log any exception
}
}
}
}

/* Program Starts Here */
public static void main(String args[]) {
Notepad app = new Notepad(); // create a new notepad application
app.setVisible(true); // set it visible, thus opening the application
}
}


Simply put, the main method is contained within the Notepad class. The Notepad class inherits from JFrame (part of the Swing GUI toolkit, it's effectively a basic window) and implements ActionListener (which gives you access to action/event related methods).

The constructor for the Notepad class contains all of the basic settings and preparations for the JFrame, as well as all of its components (it also adds the components to the content panel of the JFrame). The constructor is obviously called when the Notepad object is constructed in the main method.

The actionPerformed(ActionEvent) method is from ActionListener. It is called whenever an action or event (such as the clicking of a button) occurs to any of the components it was applied to (which in this case would be the Open, Save, and Close options of the File menu in the menubar). The body of the method is used to handle these events.

EDIT: I thought it would interest you that while a BorderLayout is applied to the JFrame (and I even mention it in the comments of the source code), it actually doesn't do anything as of the current time (as no components are ever positioned according to it).


I posted the code completely unedited in the OP. :y:

The following 2 users say thank you to tylerallmighty for this useful post:

Epic?, Renegdr

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo