Example of creating
Edit menu for Notepad:
1. import javax.swing.*;
2. import java.awt.event.*;
3.
4. public class Notepad implements ActionListener{
5. JFrame f;
6. JMenuBar mb;
7. JMenu file,edit,help;
8. JMenuItem cut,copy,paste,selectAll;
9. JTextArea ta;
10.
11. Notepad(){
12. f=new JFrame();
13.
14. cut=new JMenuItem("cut");
15. copy=new JMenuItem("copy");
16. paste=new JMenuItem("paste");
17. selectAll=new JMenuItem("selectAll");
18.
19. cut.addActionListener(this);
20. copy.addActionListener(this);
21. paste.addActionListener(this);
22. selectAll.addActionListener(this);
23.
24. mb=new JMenuBar();
25. mb.setBounds(5,5,400,40);
26.
27. file=new JMenu("File");
28. edit=new JMenu("Edit");
29. help=new JMenu("Help");
30.
31. edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);
32.
33.
34. mb.add(file);mb.add(edit);mb.add(help);
35.
36. ta=new JTextArea();
37. ta.setBounds(5,30,460,460);
38.
39. f.add(mb);f.add(ta);
40.
41. f.setLayout(null);
42. f.setSize(500,500);
43. f.setVisible(true);
44. }
45.
46. public void actionPerformed(ActionEvent e) {
47. if(e.getSource()==cut)
48. ta.cut();
49. if(e.getSource()==paste)
50. ta.paste();
51. if(e.getSource()==copy)
52. ta.copy();
53. if(e.getSource()==selectAll)
54. ta.selectAll();
55. }
56.
57. public static void main(String[] args) {
58. new Notepad();
59. }
60. }
No comments:
Post a Comment