Yurttas/PL/OOL/Java/F/05/01/05/00/EditPad00.java
Jump to navigation
Jump to search
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.*;
4
5/**
6 * Implements a simple note pad using Swing components.
7 */
8
9public class EditPad00 {
10 public static void main(String args[]) {
11 new EditPad00();
12 }
13
14 String rememberedString;
15 JTextArea textEditArea;
16
17 public EditPad00() {
18 JFrame window = new JFrame("EditPad");
19 Container contentPane = window.getContentPane();
20
21 /* Create a text area, wrap it in a scroll pane
22 and stuff it in the content pane. */
23 textEditArea = new JTextArea();
24 JScrollPane editArea = new JScrollPane(textEditArea);
25 contentPane.add(editArea, BorderLayout.CENTER);
26
27 // Make the application exit when the window is closed by the user.
28 window.addWindowListener(new WindowAdapter() {
29 public void windowClosing(WindowEvent e) {
30 System.exit(0);
31 }
32 });
33
34 // Create actions that the user can activate
35 Action clearOperation = new AbstractAction("Clear") {
36 public void actionPerformed(ActionEvent e) {
37 textEditArea.setText("");
38 }
39 };
40
41 Action rememberOperation = new AbstractAction("Remember") {
42 public void actionPerformed(ActionEvent e) {
43 rememberedString = textEditArea.getText();
44 }
45 };
46
47 Action recallOperation = new AbstractAction("Recall") {
48 public void actionPerformed(ActionEvent e) {
49 if(rememberedString == null) return;
50 textEditArea.setText(rememberedString);
51 }
52 };
53
54 // Create the toolbar and add the actions
55 JToolBar toolbar = new JToolBar();
56 toolbar.add(clearOperation);
57 toolbar.add(rememberOperation);
58 toolbar.add(recallOperation);
59
60 contentPane.add(toolbar, BorderLayout.NORTH);
61
62 // Create the edit menu and add the actions
63 JMenu editMenu = new JMenu("Edit");
64 editMenu.add(clearOperation);
65 editMenu.add(rememberOperation);
66 editMenu.add(recallOperation);
67
68 // Put the menu in the menu bar and attach it to menubar
69 JMenuBar menubar = new JMenuBar();
70 menubar.add(editMenu);
71 window.getRootPane().setJMenuBar(menubar);
72
73 window.pack();
74 window.setVisible(true);
75 }
76
77}