This Java code defines a Notepad application with a graphical user interface. It includes classes to handle menu items and toolbar buttons for common editing functions like new, open, save, copy, cut and paste. The main Notepad class extends JPanel and adds the menu bar, toolbar and text area to the frame on initialization.
1 of 7
Download to read offline
More Related Content
Notepad
1. package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
public class notepad extends JPanel
{
JTextArea jta = new JTextArea("", 24, 40);
JScrollPane jsp = new JScrollPane(jta);
JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu search = new JMenu("Search");
JMenu help = new JMenu("Help");
JMenuItem jmi;
JToolBar toolBar=new JToolBar();
Clipboard clipbd = getToolkit().getSystemClipboard();
class newL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.setDocument(new PlainDocument());
}
}
class openL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(notepad.this, "Open file");
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String file = fc.getSelectedFile().getPath();
if(file == null)
{
return;
}
try
{
Reader in = new FileReader(file);
char[] buff = new char[4096];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
{
jta.setDocument(new PlainDocument());
jta.append(new String(buff, 0, nch));
}
}
catch (IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
else
{
return;