How To Create Edit Menu For Notepad In Swing Java
Create edit menu for Notepad
We can easily create edit menu for Notepad in Swing Java.
Let’s see an example of edit menu for Notepad in Swing.
Code
- import javax.swing.*;
- import java.awt.event.*;
- public class NotepadExample implements ActionListener {
- JFrame jf;
- JMenuBar jmb;
- JMenu file, edit, help;
- JMenuItem cut, copy, paste, selectAll;
- JTextArea jta;
- NotepadExample() {
- jf = new JFrame();
- cut = new JMenuItem("Cut");
- copy = new JMenuItem("Copy");
- paste = new JMenuItem("Paste");
- selectAll = new JMenuItem("SelectAll");
- cut.addActionListener(this);
- copy.addActionListener(this);
- paste.addActionListener(this);
- selectAll.addActionListener(this);
- jmb = new JMenuBar();
- jmb.setBounds(10, 10, 300, 50);
- file = new JMenu("File");
- edit = new JMenu("Edit");
- help = new JMenu("Help");
- edit.add(cut);
- edit.add(copy);
- edit.add(paste);
- edit.add(selectAll);
- jmb.add(file);
- jmb.add(edit);
- jmb.add(help);
- jta = new JTextArea();
- jta.setBounds(5, 30, 460, 460);
- jf.add(jmb);
- jf.add(jta);
- jf.setLayout(null);
- jf.setSize(500, 500);
- jf.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == cut) {
- jta.cut();
- }
- if (e.getSource() == paste) {
- jta.paste();
- }
- if (e.getSource() == copy) {
- jta.copy();
- }
- if (e.getSource() == selectAll) {
- jta.selectAll();
- }
- }
- public static void main(String[] args) {
- new NotepadExample();
- }
- }
Output
Summary
Thus, we learnt how to create edit menu for Notepad in Swing Java.