menuLookDemo/scala/MenuLookDemo.scala

  1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
  2: /*
  3:  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  4:  */ 
  5: package components
  6: 
  7: import java.awt.Dimension
  8: import java.awt.event.ActionEvent
  9: import java.awt.event.KeyEvent
 10: import javax.swing.ImageIcon
 11: import javax.swing.KeyStroke
 12: import swing._
 13: import swing.event._
 14: import BorderPanel._
 15: 
 16: // ----------------------------------------
 17: object MenuLookDemo {
 18:   val version =          MenuLookDemo
 19:     .getClass.getName+": #1.0.17"
 20: }
 21: 
 22: // ----------------------------------------
 23: /*
 24:  * This class exists solely to show you what menus look like.
 25:  * It has no menu-related event handling.
 26:  */
 27: class MenuLookDemo(val frame: Frame) extends FlowPanel {
 28: 
 29:   def createMenuBar = {
 30:     //Create the menu bar.
 31:     val menuBar = new MenuBar
 32: 
 33:     //Build the first menu.
 34:     val menu = new Menu(
 35:       "A Menu") {
 36:       mnemonic = Key.A
 37:       peer.getAccessibleContext().setAccessibleDescription(
 38:         "The only menu in this program that has menu items")
 39:       menuBar.contents += this
 40:     }
 41: 
 42:     //a group of JMenuItems
 43:     new MenuItem(
 44:       "A text-only menu item") {
 45:       mnemonic = Key.T
 46:       peer.setAccelerator(KeyStroke.getKeyStroke(
 47:         KeyEvent.VK_1, ActionEvent.ALT_MASK))
 48:       peer.getAccessibleContext().setAccessibleDescription(
 49:         "This doesn't really do anything")
 50:       menu.contents += this
 51:     }
 52: 
 53:     val icon_ = createImageIcon("images/middle.gif")
 54:     new MenuItem(
 55:       "Both text and icon") {
 56:       icon = icon_
 57:       mnemonic = Key.B
 58:       menu.contents += this
 59:     }
 60: 
 61:     new MenuItem(
 62:       "") {
 63:       icon = icon_
 64:       mnemonic = Key.D
 65:       menu.contents += this
 66:     }
 67: 
 68:     //a group of radio button menu items
 69:     menu.peer.addSeparator()
 70:     val group = new ButtonGroup
 71: 
 72:     new RadioMenuItem(
 73:       "A radio button menu item") {
 74:       selected = true
 75:       mnemonic = Key.R
 76:       group.buttons += this
 77:       menu.contents += this
 78:     }
 79:     new RadioMenuItem(
 80:       "Another one") {
 81:       mnemonic = Key.O
 82:       group.buttons += this
 83:       menu.contents += this
 84:     }
 85: 
 86:     //a group of check box menu items
 87:     menu.peer.addSeparator()
 88:     new CheckMenuItem(
 89:       "A check box menu item") {
 90:       mnemonic = Key.C
 91:       menu.contents += this
 92:     }
 93:     new CheckMenuItem(
 94:       "Another one") {
 95:       mnemonic = Key.H
 96:       menu.contents += this
 97:     }
 98: 
 99:     //a submenu
100:     menu.peer.addSeparator()
101:     val submenu = new Menu(
102:       "A submenu") {
103:       mnemonic = Key.S
104:     }
105: 
106:     new MenuItem(
107:       "An item in the submenu") {
108:       peer.setAccelerator(KeyStroke.getKeyStroke(
109:         KeyEvent.VK_2, ActionEvent.ALT_MASK))
110:       submenu.contents += this
111:     }
112:     new MenuItem(
113:       "Another item") {
114:       submenu.contents += this
115:     }
116:     menu.contents += submenu
117: 
118:     //Build second menu in the menu bar.
119:     new Menu(
120:       "Another Menu") {
121:       mnemonic = Key.N
122:       peer.getAccessibleContext().setAccessibleDescription(
123:         "This menu does nothing");
124:       menuBar.contents += this
125:     }
126:     menuBar
127:   }
128: 
129:   def createContentPane = {
130:     //Create the content-pane-to-be.
131:     val contentPane = new BorderPanel {
132:       opaque = true
133: 
134:       //Create a scrolled text area.
135:       val output = new TextArea(5, 30) {
136:         editable = false
137:       }
138:       val scrollPane = new ScrollPane(output)
139: 
140:       //Add the text area to the content pane.
141:       add(scrollPane, Position.Center)
142:     }
143:     contentPane
144:   }
145: 
146:   // ----------------------------------------
147:   /** Returns an ImageIcon, or null if the path was invalid. */
148:   def createImageIcon(path: String): ImageIcon = {
149:     val imgURL = MenuLookDemo.getClass.getResource(path)
150:     if (imgURL != null) {
151:       return new ImageIcon(imgURL)
152:     } else {
153:       System.err.println("Couldn't find file: " + path)
154:       return null
155:     }
156:   }
157: 
158:   // ----------------------------------------
159:   //Create and set up the content pane.
160:   frame.menuBar = createMenuBar
161:   contents += createContentPane
162: 
163:   //Display the window.
164:   frame.size = new Dimension(450, 260)
165: }
166: 
167: // ========================================