-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notepad.java
106 lines (92 loc) · 2.79 KB
/
Notepad.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Notepad implements ActionListener
{
JFrame frame;
JMenuBar mb;
JMenu edit, help, paste;
JTextArea text;
JMenuItem copy, cut, pasteHere, pasteEnd, selectAll, clear, about;
public void actionPerformed(ActionEvent ae)
{
JFrame nFrame = new JFrame();
if( ae.getSource() == copy )
{
text.copy();
JOptionPane.showMessageDialog(nFrame, "Copied to Clipboard");
}
if( ae.getSource() == cut )
{
text.cut();
JOptionPane.showMessageDialog(nFrame, "Cut to Clipboard");
}
if( ae.getSource() == pasteHere )
text.paste();
if( ae.getSource() == pasteEnd )
{
String currentText = text.getText(); // extract the text in the editor
text.setText(""); // empty the editor
text.paste(); // paste the content of the selected text in the editor
String clipboardText = text.getText(); // extract the selected text
text.setText(currentText+clipboardText); // concatenate them and display
}
if( ae.getSource() == selectAll )
text.selectAll();
if( ae.getSource() == clear )
{
text.setText("");
JOptionPane.showMessageDialog(nFrame, "Editor Cleared");
}
if( ae.getSource() == about )
JOptionPane.showMessageDialog(nFrame, "Rohit's Text Editor\nVerion 1.0\nRelease 2018");
}
Notepad()
{
frame = new JFrame("My Text Editor");
text = new JTextArea();
text.setBounds(20,20,440,400);
text.setBorder(BorderFactory.createDashedBorder(Color.BLUE));
text.setLineWrap(true);
mb = new JMenuBar();
edit = new JMenu("Edit");
copy = new JMenuItem("Copy");
cut = new JMenuItem("Cut");
paste = new JMenu("Paste");
pasteHere = new JMenuItem("Paste at Cursor");
pasteEnd = new JMenuItem("Paste at End");
paste.add(pasteHere);
paste.add(pasteEnd);
selectAll = new JMenuItem("Select All");
clear = new JMenuItem("Clear");
edit.add(copy);
edit.add(cut);
edit.add(paste);
edit.add(selectAll);
edit.add(clear);
help = new JMenu("Help");
about = new JMenuItem("About the Editor");
help.add(about);
mb.add(edit);
mb.add(help);
frame.add(text);
frame.add(mb);
frame.setJMenuBar(mb);
copy.addActionListener(this);
cut.addActionListener(this);
pasteHere.addActionListener(this);
pasteEnd.addActionListener(this);
selectAll.addActionListener(this);
clear.addActionListener(this);
about.addActionListener(this);
frame.setSize( 500, 500);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new Notepad();
}
}