Advertisement
mrkarp

Untitled

Oct 7th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. package Lab13;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. public class NoteTaker extends JFrame {
  9. // constants for set up of note taking area
  10. public static final int WIDTH = 600;
  11. public static final int HEIGHT = 300;
  12. public static final int LINES = 13;
  13. public static final int CHAR_PER_LINE = 45;
  14.  
  15. // objects in GUI
  16. private JTextArea theText; // area to take notes
  17. private JMenuBar mBar; // horizontal menu bar
  18. private JPanel textPanel; // panel to hold scrolling text area
  19. private JMenu notesMenu; // vertical menu with choices for notes
  20.  
  21. // ****THESE ITEMS ARE NOT YET USED. YOU WILL BE CREATING THEM IN THIS LAB
  22. private JMenu viewMenu; // vertical menu with choices for views
  23. private JMenu lafMenu; // vertical menu with look and feel
  24. private JMenu sbMenu; // vertical menu with scroll bar option
  25. private JScrollPane scrolledText;// scroll bars
  26.  
  27. // default notes
  28. private String note1 = "No Note 1.";
  29. private String note2 = "No Note 2.";
  30.  
  31. /** constructor */
  32. public NoteTaker() {
  33. // create a closeable JFrame with a specific size
  34. super("Note Taker");
  35. setSize(WIDTH, HEIGHT);
  36. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.  
  38. // get contentPane and set layout of the window
  39. Container contentPane = getContentPane();
  40. contentPane.setLayout(new BorderLayout());
  41.  
  42. // creates the vertical menus
  43. createNotes();
  44. createViews();
  45.  
  46. // creates horizontal menu bar and
  47. // adds vertical menus to it
  48. mBar = new JMenuBar();
  49. mBar.add(notesMenu);
  50. // Add the viewMenu to the menuBar
  51. mBar.add(viewMenu);
  52. // ****ADD THE viewMenu TO THE MENU BAR HERE
  53. setJMenuBar(mBar);
  54.  
  55. // creates a panel to take notes on
  56. textPanel = new JPanel();
  57. textPanel.setBackground(Color.blue);
  58. theText = new JTextArea(LINES, CHAR_PER_LINE);
  59. theText.setBackground(Color.white);
  60. // JScrollPane Object named scrolledText
  61. scrolledText = new JScrollPane(theText);
  62. // ****AND PASS IN theText, THEN
  63. // ****CHANGE THE LINE BELOW BY PASSING IN scrolledText
  64. textPanel.add(theText);
  65. textPanel.add(scrolledText);
  66. contentPane.add(textPanel, BorderLayout.CENTER);
  67. }
  68.  
  69. /**
  70. * creates vertical menu associated with Notes menu item on menu bar
  71. */
  72. public void createNotes() {
  73. notesMenu = new JMenu("Notes");
  74. JMenuItem item;
  75.  
  76. item = new JMenuItem("Save Note 1");
  77. item.addActionListener(new MenuListener());
  78. notesMenu.add(item);
  79.  
  80. item = new JMenuItem("Save Note 2");
  81. item.addActionListener(new MenuListener());
  82. notesMenu.add(item);
  83.  
  84. item = new JMenuItem("Open Note 1");
  85. item.addActionListener(new MenuListener());
  86. notesMenu.add(item);
  87.  
  88. item = new JMenuItem("Open Note 2");
  89. item.addActionListener(new MenuListener());
  90. notesMenu.add(item);
  91.  
  92. item = new JMenuItem("Clear");
  93. item.addActionListener(new MenuListener());
  94. notesMenu.add(item);
  95.  
  96. item = new JMenuItem("Exit");
  97. item.addActionListener(new MenuListener());
  98. notesMenu.add(item);
  99. }
  100.  
  101. /**
  102. * creates vertical menu associated with Views menu item on the menu bar
  103. */
  104. public void createViews() {
  105. viewMenu = new JMenu("Views");
  106. JMenuItem item;
  107.  
  108. lafMenu = new JMenu("Look and Feel");
  109. lafMenu.addActionListener(new MenuListener());
  110. viewMenu.add(lafMenu);
  111.  
  112. item = new JMenuItem("Metal");
  113. item.addActionListener(new MenuListener());
  114. lafMenu.add(item);
  115.  
  116. item = new JMenuItem("Motif");
  117. item.addActionListener(new MenuListener());
  118. lafMenu.add(item);
  119.  
  120. item = new JMenuItem("Windows");
  121. item.addActionListener(new MenuListener());
  122. lafMenu.add(item);
  123.  
  124. sbMenu = new JMenu("Scroll Bars");
  125. sbMenu.addActionListener(new MenuListener());
  126. viewMenu.add(sbMenu);
  127.  
  128. item = new JMenuItem("Never");
  129. item.addActionListener(new MenuListener());
  130. sbMenu.add(item);
  131.  
  132. item = new JMenuItem("Always");
  133. item.addActionListener(new MenuListener());
  134. sbMenu.add(item);
  135.  
  136. item = new JMenuItem("As Needed");
  137. item.addActionListener(new MenuListener());
  138. sbMenu.add(item);
  139. }
  140.  
  141. /** creates the look and feel submenu */
  142. public void createLookAndFeel() {
  143.  
  144. }
  145.  
  146. /** creates the scroll bars submenu */
  147. public void createScrollBars() {
  148.  
  149. }
  150.  
  151. private class MenuListener implements ActionListener {
  152.  
  153. public void actionPerformed(ActionEvent e) {
  154. String actionCommand = e.getActionCommand();
  155. String laf = "";
  156. if (actionCommand.equals("Save Note 1"))
  157. note1 = theText.getText();
  158. else if (actionCommand.equals("Save Note 2"))
  159. note2 = theText.getText();
  160. else if (actionCommand.equals("Clear"))
  161. theText.setText("");
  162. else if (actionCommand.equals("Open Note 1"))
  163. theText.setText(note1);
  164. else if (actionCommand.equals("Open Note 2"))
  165. theText.setText(note2);
  166. else if (actionCommand.equals("Exit"))
  167. System.exit(0);
  168. // Actions for scrollBars
  169. else if (actionCommand.equals("Never"))
  170. scrolledText
  171. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
  172. else if (actionCommand.equals("Always"))
  173. scrolledText
  174. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  175. else if (actionCommand.equals("As Needed"))
  176. scrolledText
  177. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  178. // Actions for LookAndFeel
  179. else if (actionCommand.equals("Metal"))
  180. laf = "javax.swing.plaf.metal.MetalLookAndFeel";
  181. else if (actionCommand.equals("Motif"))
  182. laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  183. else if (actionCommand.equals("Windows"))
  184. laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  185. try {
  186. UIManager.setLookAndFeel(laf);
  187. SwingUtilities.updateComponentTreeUI(rootPane);
  188. } catch (Exception excep) {
  189.  
  190. theText.setText("Error in memo interface");
  191.  
  192. }
  193. }
  194. }
  195.  
  196. public static void main(String[] args) {
  197. NoteTaker gui = new NoteTaker();
  198. gui.setVisible(true);
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement