0% found this document useful (0 votes)
95 views6 pages

Code For notepa-WPS Office

The code defines a Notepad class that extends JFrame and implements ActionListener. It contains a TextArea to display text and menus for File, Open, Save, and Close options. When the application starts, a Notepad instance is created with the default size and title. Menu options are added to open, save, and close files by reading/writing to files and clearing/setting the TextArea text.

Uploaded by

Hafza Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views6 pages

Code For notepa-WPS Office

The code defines a Notepad class that extends JFrame and implements ActionListener. It contains a TextArea to display text and menus for File, Open, Save, and Close options. When the application starts, a Notepad instance is created with the default size and title. Menu options are added to open, save, and close files by reading/writing to files and clearing/setting the TextArea text.

Uploaded by

Hafza Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code for notepad project:

package notepad;

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 static final long serialVersionUID = 1L;

private TextArea textArea = new TextArea("", 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 Notepad() {

this.setSize(500, 300);

this.setTitle("Java Notepad Tutorial");

setDefaultCloseOperation(EXIT_ON_CLOSE);

this.textArea.setFont(new Font("Bell MT", Font.ITALIC, 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);

You might also like