效果如图: 新建NewSampleApplication应用程序代码,清单如下: 1 package com.javaapp.ch12; 2 3 //导入所需类包 4 import java.awt.BorderLayout; 5 6 import javax.swing.ActionMap; 7 import javax.swing.JButton; 8 import javax
效果如图:
新建NewSampleApplication应用程序代码,清单如下:
1 package com.javaapp.ch12;
2
3 //导入所需类包
4 import java.awt.BorderLayout;
5
6 import javax.swing.ActionMap;
7 import javax.swing.JButton;
8 import javax.swing.JMenu;
9 import javax.swing.JMenuBar;
10 import javax.swing.JMenuItem;
11 import javax.swing.JPanel;
12 import javax.swing.JSeparator;
13 import javax.swing.JToolBar;
14
15 import org.jdesktop.application.Action;
16 import org.jdesktop.application.Application;
17 import org.jdesktop.application.SingleFrameApplication;
18
19 /**
20 * 继承于只有一个主窗口的SingleFrameApplication
21 */
22 public class NewSampleApplication extends SingleFrameApplication {
23 //定义组建变量
24 private JMenuBar menuBar;
25 private JPanel topPanel;
26 private JMenuItem jMenuItem7;
27 private JMenuItem jMenuItem6;
28 private JMenuItem jMenuItem5;
29 private JMenuItem jMenuItem4;
30 private JMenu editMenu;
31 private JMenuItem jMenuItem3;
32 private JMenuItem jMenuItem2;
33 private JMenuItem jMenuItem1;
34 private JMenu fileMenu;
35 private JSeparator jSeparator;
36 private JButton saveButton;
37 private JButton openButton;
38 private JButton newButton;
39 private JToolBar toolBar;
40 private JPanel toolBarPanel;
41 private JPanel contentPanel;
42 //用@Action 标注定义动作
43 @Action
44 public void open() {
45 //添加事件代码
46 }
47
48 @Action
49 public void save() {
50 }
51
52 @Action
53 public void newFile() {
54 }
55 //使用ApplicationContext的getActionMap方法来建立包含open和close的ActionMap
56 private ActionMap getAppActionMap() {
57 return Application.getInstance().getContext().getActionMap(this);
58 }
59
60 @Override
61 protected void startup() { //扩展Application类并覆盖startup方法。startup方法应当建立和显示应用程序出事的GUI。
62 {
63 topPanel = new JPanel();
64 BorderLayout panelLayout = new BorderLayout();
65 topPanel.setLayout(panelLayout);
66 topPanel.setPreferredSize(new java.awt.Dimension(500, 300));
67 {
68 contentPanel = new JPanel();
69 topPanel.add(contentPanel, BorderLayout.CENTER);
70 }
71 {
72 toolBarPanel = new JPanel();
73 topPanel.add(toolBarPanel, BorderLayout.NORTH);
74 BorderLayout jPanel1Layout = new BorderLayout();
75 toolBarPanel.setLayout(jPanel1Layout);
76 {
77 toolBar = new JToolBar();
78 toolBarPanel.add(toolBar, BorderLayout.CENTER);
79 {
80 newButton = new JButton();
81 toolBar.add(newButton);
82 newButton.setAction(getAppActionMap().get("newFile"));
83 newButton.setName("newButton");
84 newButton.setFocusable(false);
85 }
86 {
87 openButton = new JButton();
88 toolBar.add(openButton);
89 openButton.setAction(getAppActionMap().get("open"));
90 openButton.setName("openButton");
91 openButton.setFocusable(false);
92 }
93 {
94 saveButton = new JButton();
95 toolBar.add(saveButton);
96 saveButton.setAction(getAppActionMap().get("save"));
97 saveButton.setName("saveButton");
98 saveButton.setFocusable(false);
99 }
100 }
101 {
102 jSeparator = new JSeparator();
103 toolBarPanel.add(jSeparator, BorderLayout.SOUTH);
104 }
105 }
106 }
107 menuBar = new JMenuBar();
108 {
109 fileMenu = new JMenu();
110 menuBar.add(fileMenu);
111 fileMenu.setName("fileMenu");
112 {
113 jMenuItem1 = new JMenuItem();
114 fileMenu.add(jMenuItem1);
115 jMenuItem1.setAction(getAppActionMap().get("newFile"));
116 }
117 {
118 jMenuItem2 = new JMenuItem();
119 fileMenu.add(jMenuItem2);
120 jMenuItem2.setAction(getAppActionMap().get("open"));
121 }
122 {
123 jMenuItem3 = new JMenuItem();
124 fileMenu.add(jMenuItem3);
125 jMenuItem3.setAction(getAppActionMap().get("save"));
126 }
127 }
128 {
129 editMenu = new JMenu();
130 menuBar.add(editMenu);
131 editMenu.setName("editMenu");
132 {
133 jMenuItem4 = new JMenuItem();
134 editMenu.add(jMenuItem4);
135 jMenuItem4.setAction(getAppActionMap().get("copy"));
136 }
137 {
138 jMenuItem5 = new JMenuItem();
139 editMenu.add(jMenuItem5);
140 jMenuItem5.setAction(getAppActionMap().get("cut"));
141 }
142 {
143 jMenuItem6 = new JMenuItem();
144 editMenu.add(jMenuItem6);
145 jMenuItem6.setAction(getAppActionMap().get("paste"));
146 }
147 {
148 jMenuItem7 = new JMenuItem();
149 editMenu.add(jMenuItem7);
150 jMenuItem7.setAction(getAppActionMap().get("delete"));
151 }
152 }
153 getMainFrame().setJMenuBar(menuBar);
154 show(topPanel);
155 }
156
157 public static void main(String[] args) {
158 //在main方法中,调用Application.launch方法。这将构建并启动在事件分派线程上的Application子类
159 launch(NewSampleApplication.class, args);
160 }
161
162 }
程序分析:
在上面程序中,用@Action 标注作为Action的actionPerformed方法的方法。ApplicationContext.getActionMap方法建立了包含由某些类定义的每个@Action的Action对象的ActionMap。ActionMap将父类连接起来,并且每个Application的子类都有父类。这样,所有的应用程序继承了从Application基类定义的cut、copy、paste、delete和quit动作。使用ApplicationContext的getActionMap方法来建立包含save、newFile、open的ActionMap。
【文章原创作者:盐城网页设计 http://www.1234xp.com/yancheng.html 处的文章,转载请说明出处】