java工具栏_Java基础之处理事件——添加工具栏(Sketcher 7 with File toolbar buttons)...

1 //Frame for the Sketcher application

2 import javax.swing.*;3 import javax.swing.border.*;4 import java.awt.event.*;5 import java.awt.*;6

7 import static java.awt.event.InputEvent.*;8 import static java.awt.AWTEvent.*;9 import static java.awt.Color.*;10 import static Constants.SketcherConstants.*;11 import static javax.swing.Action.*;12

13 @SuppressWarnings("serial")14 public class SketcherFrame extendsJFrame {15 //Constructor

16 publicSketcherFrame(String title) {17 setTitle(title); //Set the window title

18 setJMenuBar(menuBar); //Add the menu bar to the window

19 setDefaultCloseOperation(EXIT_ON_CLOSE); //Default is exit the application

20

21 createFileMenu(); //Create the File menu

22 createElementMenu(); //Create the element menu

23 createColorMenu(); //Create the element menu

24 createToolbar();25 toolBar.setRollover(true);26 getContentPane().add(toolBar, BorderLayout.NORTH);27 }28

29 //Create File menu item actions

30 private voidcreateFileMenuActions() {31 newAction = new FileAction("New", 'N', CTRL_DOWN_MASK);32 openAction = new FileAction("Open", 'O', CTRL_DOWN_MASK);33 closeAction = new FileAction("Close");34 saveAction = new FileAction("Save", 'S', CTRL_DOWN_MASK);35 saveAsAction = new FileAction("Save As...");36 printAction = new FileAction("Print", 'P', CTRL_DOWN_MASK);37 exitAction = new FileAction("Exit", 'X', CTRL_DOWN_MASK);38

39 //Initialize the array

40 FileAction[] actions ={openAction, closeAction, saveAction, saveAsAction, printAction, exitAction};41 fileActions =actions;42

43 //Add toolbar icons

44 newAction.putValue(LARGE_ICON_KEY, NEW24);45 openAction.putValue(LARGE_ICON_KEY, OPEN24);46 saveAction.putValue(LARGE_ICON_KEY, SAVE24);47 saveAsAction.putValue(LARGE_ICON_KEY, SAVEAS24);48 printAction.putValue(LARGE_ICON_KEY, PRINT24);49 }50

51 //Create the File menu

52 private voidcreateFileMenu() {53 JMenu fileMenu = new JMenu("File"); //Create File menu

54 fileMenu.setMnemonic('F'); //Create shortcut

55 createFileMenuActions(); //Create Actions for File menu item56

57 //Construct the file drop-down menu

58 fileMenu.add(newAction); //New Sketch menu item

59 fileMenu.add(openAction); //Open sketch menu item

60 fileMenu.add(closeAction); //Close sketch menu item

61 fileMenu.addSeparator(); //Add separator

62 fileMenu.add(saveAction); //Save sketch to file

63 fileMenu.add(saveAsAction); //Save As menu item

64 fileMenu.addSeparator(); //Add separator

65 fileMenu.add(printAction); //Print sketch menu item

66 fileMenu.addSeparator(); //Add separator

67 fileMenu.add(exitAction); //Print sketch menu item

68 menuBar.add(fileMenu); //Add the file menu

69 }70

71 //Create Element menu actions

72 private voidcreateElementTypeActions() {73 lineAction = new TypeAction("Line", LINE, 'L', CTRL_DOWN_MASK);74 rectangleAction = new TypeAction("Rectangle", RECTANGLE, 'R', CTRL_DOWN_MASK);75 circleAction = new TypeAction("Circle", CIRCLE,'C', CTRL_DOWN_MASK);76 curveAction = new TypeAction("Curve", CURVE,'U', CTRL_DOWN_MASK);77

78 //Initialize the array

79 TypeAction[] actions ={lineAction, rectangleAction, circleAction, curveAction};80 typeActions =actions;81 }82

83 //Create the Elements menu

84 private voidcreateElementMenu() {85 createElementTypeActions();86 JMenu elementMenu = new JMenu("Elements"); //Create Elements menu

87 elementMenu.setMnemonic('E'); //Create shortcut

88 createRadioButtonDropDown(elementMenu, typeActions, lineAction);89 menuBar.add(elementMenu); //Add the element menu

90 }91

92 //Create Color menu actions

93 private voidcreateElementColorActions() {94 redAction = new ColorAction("Red", RED, 'R', CTRL_DOWN_MASK|ALT_DOWN_MASK);95 yellowAction = new ColorAction("Yellow", YELLOW, 'Y', CTRL_DOWN_MASK|ALT_DOWN_MASK);96 greenAction = new ColorAction("Green", GREEN, 'G', CTRL_DOWN_MASK|ALT_DOWN_MASK);97 blueAction = new ColorAction("Blue", BLUE, 'B', CTRL_DOWN_MASK|ALT_DOWN_MASK);98

99 //Initialize the array

100 ColorAction[] actions ={redAction, greenAction, blueAction, yellowAction};101 colorActions =actions;102 }103

104 //Create the Color menu

105 private voidcreateColorMenu() {106 createElementColorActions();107 JMenu colorMenu = new JMenu("Color"); //Create Elements menu

108 colorMenu.setMnemonic('C'); //Create shortcut

109 createRadioButtonDropDown(colorMenu, colorActions, blueAction);110 menuBar.add(colorMenu); //Add the color menu

111 }112

113 //Menu creation helper

114 private voidcreateRadioButtonDropDown(JMenu menu, Action[] actions, Action selected) {115 ButtonGroup group = newButtonGroup();116 JRadioButtonMenuItem item = null;117 for(Action action : actions) {118 group.add(menu.add(item = newJRadioButtonMenuItem(action)));119 if(action ==selected) {120 item.setSelected(true); //This is default selected

121 }122 }123 }124

125 //Create toolbar buttons on the toolbar

126 private voidcreateToolbar() {127 for(FileAction action: fileActions){128 if(action != exitAction && action !=closeAction)129 addToolbarButton(action); //Add the toolbar button

130 }131 }132

133 //Create and add a toolbar button

134 private voidaddToolbarButton(Action action) {135 JButton button = new JButton(action); //Create from Action

136 button.setBorder(BorderFactory.createCompoundBorder( //Add button border

137 new EmptyBorder(2,5,5,2), //Outside border

138 BorderFactory.createRaisedBevelBorder())); //Inside border

139 button.setHideActionText(true); //No label on the button

140 toolBar.add(button); //Add the toolbar button

141 }142

143 //Inner class defining Action objects for File menu items

144 class FileAction extendsAbstractAction {145 //Create action with a name

146 FileAction(String name) {147 super(name);148 }149

150 //Create action with a name and accelerator

151 FileAction(String name, char ch, intmodifiers) {152 super(name);153 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));154

155 //Now find the character to underline

156 int index =name.toUpperCase().indexOf(ch);157 if(index != -1) {158 putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);159 }160 }161

162 //Event handler

163 public voidactionPerformed(ActionEvent e) {164 //You will add action code here eventually...

165 }166 }167

168 //Inner class defining Action objects for Element type menu items

169 class TypeAction extendsAbstractAction {170 //Create action with just a name property

171 TypeAction(String name, inttypeID) {172 super(name);173 this.typeID =typeID;174 }175

176 //Create action with a name and an accelerator

177 private TypeAction(String name,int typeID, char ch, intmodifiers) {178 this(name, typeID);179 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));180

181 //Now find the character to underline

182 int index =name.toUpperCase().indexOf(ch);183 if(index != -1) {184 putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);185 }186 }187

188 public voidactionPerformed(ActionEvent e) {189 elementType =typeID;190 }191

192 private inttypeID;193 }194

195 //Handles color menu items

196 class ColorAction extendsAbstractAction {197 //Create an action with a name and a color

198 publicColorAction(String name, Color color) {199 super(name);200 this.color =color;201 }202

203 //Create an action with a name, a color, and an accelerator

204 public ColorAction(String name, Color color, char ch, intmodifiers) {205 this(name, color);206 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));207

208 //Now find the character to underline

209 int index =name.toUpperCase().indexOf(ch);210 if(index != -1) {211 putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);212 }213 }214

215 public voidactionPerformed(ActionEvent e) {216 elementColor =color;217 }218

219 privateColor color;220 }221

222 //File actions

223 privateFileAction newAction, openAction, closeAction, saveAction, saveAsAction, printAction, exitAction;224 private FileAction[] fileActions; //File actions as an array225

226 //Element type actions

227 privateTypeAction lineAction, rectangleAction, circleAction, curveAction;228 private TypeAction[] typeActions; //Type actions as an array229

230 //Element color actions

231 privateColorAction redAction, yellowAction,greenAction, blueAction;232 private ColorAction[] colorActions; //Color actions as an array

233

234 private JMenuBar menuBar = new JMenuBar(); //Window menu bar

235 private Color elementColor = DEFAULT_ELEMENT_COLOR; //Current element color

236 private int elementType = DEFAULT_ELEMENT_TYPE; //Current element type

237 private JToolBar toolBar = new JToolBar(); //Window toolbar

238 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值