在學習了Java事件之后,自己寫了一個極其簡單的記事本。用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
package com.package3; /* * 功能:簡易記事本的開發,可以保存文件,打開文件,退出記事本 * author:ywq */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class MenuText { //定義組件: JFrame f; MenuBar mb; //菜單欄 Menu mu; //菜單 JTextArea jta; MenuItem openItem, saveItem, closeItem; //子菜單 FileDialog openDia,saveDia; //彈出的保存和打開框 File file; //構造函數 public MenuText() { //調用初始化函數 init(); } //對組件進行初始化操作 public void init() { f= new JFrame( "簡易記事本" ); mb= new MenuBar(); mu= new Menu( "文件" ); openItem= new MenuItem( "打開" ); saveItem= new MenuItem( "保存" ); closeItem= new MenuItem( "退出" ); jta= new JTextArea(); f.add(jta); //添加 mu.add(openItem); mu.add(saveItem); mu.add(closeItem); mb.add(mu); f.setMenuBar(mb); openDia= new FileDialog(f, "打開" , FileDialog.LOAD); saveDia= new FileDialog(f, "保存" , FileDialog.SAVE); //設置JFrame屬性 f.setBounds( 200 , 300 , 500 , 400 ); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible( true ); //調用事件函數 event(); } //事件函數,對事件進行處理 public void event() { //打開選項 openItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //調用打開文件的方法 openFile(); } }); //保存選項 saveItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //調用保存文件的方法。 saveFile(); } }); //添加一個文本區域的事件,即按下Ctrl+S可以保存 //因為鍵盤監聽事件有多種方法,而我們只需要其中的一種,所以可以使用適配器KeyAdapter, //從而只需要實現一種方法即可 jta.addKeyListener( new KeyAdapter() { //鍵盤按下方法 public void keyPressed(KeyEvent e){ if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) { //調用保存文件的方法。 saveFile(); //JOptionPane.showMessageDialog(null, "對啦"); } } }); //關閉選項 closeItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //退出系統 System.exit( 0 ); } }); } //打開文本的方法 public void openFile() { openDia.setVisible( true ); //設置其顯示出來 //獲取路徑和文件名 String dirPath=openDia.getDirectory(); String fileName=openDia.getFile(); //防止點擊取消報錯 if (dirPath== null || fileName== null ) return ; jta.setText( "" ); //將文本區域清空 file= new File(dirPath,fileName); //建立文件對象 //按照行來讀取數據,顯示在文本區域 try { BufferedReader br = new BufferedReader( new FileReader(file)); String line = null ; while ((line=br.readLine())!= null ) { jta.append(line+ "\r\n" ); } br.close(); } catch (IOException ex) { throw new RuntimeException( "讀取失敗" ); } } //保存文本的方法。 public void saveFile() { //先判斷文件是否存在 if (file== null ) { saveDia.setVisible( true ); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); //防止點擊取消報錯 if (dirPath== null || fileName== null ) return ; //因為文件不存在。所以需要建立file對象 file = new File(dirPath,fileName); } //將數據寫入文件 try { BufferedWriter bw= new BufferedWriter( new FileWriter(file)); String info=jta.getText(); //得到文本區域的信息 bw.write(info); //寫入操作 bw.flush(); bw.close(); } catch (IOException e1) { throw new RuntimeException(); } } public static void main(String[] args) { //創建對象 new MenuText(); } } |
運行結果如圖所示:
本程序實現的功能有:
(1)可以打開某個文件,并且可以編輯。
(2)可以保存編輯后的文件。
(3)可以使用Ctrl+S來保存文本
(4)可以點擊closeItem來退出程序。
在實現功能3時,對文本區域添加了KeyListener,利用了適配器KeyAdapter來實現監聽。但是現在需要的時一種組合監聽,即ctrl和S都被按下時才會觸發保存操作。
關于組合監聽,Java API中有提供相應方法。
找到KeyEvent類的直接父類即InputEvent類。如圖所示:
查看InputEvent類中的方法,如下所示:
做為InputEvent類的子類KeyEvent類的對象e可以直接調用上邊方法來進行判斷。isControlDown()方法用來判斷ctrl鍵是否被按下。如程序中的if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S)便實現了組合判斷。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_25827845/article/details/51447029