forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBook.java
More file actions
executable file
·227 lines (225 loc) · 8.63 KB
/
AddressBook.java
File metadata and controls
executable file
·227 lines (225 loc) · 8.63 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* */ package ch17;
/* */
/* */ import java.awt.BorderLayout;
/* */ import java.awt.GridLayout;
/* */ import java.awt.event.ActionEvent;
/* */ import java.awt.event.ActionListener;
/* */ import java.io.IOException;
/* */ import java.io.RandomAccessFile;
/* */ import javax.swing.JButton;
/* */ import javax.swing.JFrame;
/* */ import javax.swing.JLabel;
/* */ import javax.swing.JPanel;
/* */ import javax.swing.JTextField;
/* */ import javax.swing.border.BevelBorder;
/* */
/* */
/* */ public class AddressBook
/* */ extends JFrame
/* */ {
/* */ static final int NAME_SIZE = 32;
/* */ static final int STREET_SIZE = 32;
/* */ static final int CITY_SIZE = 20;
/* 23 */ private JTextField jtfName = new JTextField(32); static final int STATE_SIZE = 2; static final int ZIP_SIZE = 5; static final int RECORD_SIZE = 91; private RandomAccessFile raf;
/* 24 */ private JTextField jtfStreet = new JTextField(32);
/* 25 */ private JTextField jtfCity = new JTextField(20);
/* 26 */ private JTextField jtfState = new JTextField(2);
/* 27 */ private JTextField jtfZip = new JTextField(5);
/* */
/* */
/* 30 */ private JButton jbtAdd = new JButton("Add");
/* 31 */ private JButton jbtFirst = new JButton("First");
/* 32 */ private JButton jbtNext = new JButton("Next");
/* 33 */ private JButton jbtPrevious = new JButton("Previous");
/* 34 */ private JButton jbtLast = new JButton("Last");
/* */
/* */
/* */ public AddressBook() {
/* */ try {
/* 39 */ this.raf = new RandomAccessFile("address.dat", "rw");
/* */ }
/* 41 */ catch (IOException ex) {
/* 42 */ System.out.print("Error: " + ex);
/* 43 */ System.exit(1);
/* */ }
/* */
/* */
/* 47 */ JPanel p1 = new JPanel();
/* 48 */ p1.setLayout(new GridLayout(3, 1));
/* 49 */ p1.add(new JLabel("Name"));
/* 50 */ p1.add(new JLabel("Street"));
/* 51 */ p1.add(new JLabel("City"));
/* */
/* */
/* 54 */ JPanel jpState = new JPanel();
/* 55 */ jpState.setLayout(new BorderLayout());
/* 56 */ jpState.add(new JLabel("State"), "West");
/* 57 */ jpState.add(this.jtfState, "Center");
/* */
/* */
/* 60 */ JPanel jpZip = new JPanel();
/* 61 */ jpZip.setLayout(new BorderLayout());
/* 62 */ jpZip.add(new JLabel("Zip"), "West");
/* 63 */ jpZip.add(this.jtfZip, "Center");
/* */
/* */
/* 66 */ JPanel p2 = new JPanel();
/* 67 */ p2.setLayout(new BorderLayout());
/* 68 */ p2.add(jpState, "West");
/* 69 */ p2.add(jpZip, "Center");
/* */
/* */
/* 72 */ JPanel p3 = new JPanel();
/* 73 */ p3.setLayout(new BorderLayout());
/* 74 */ p3.add(this.jtfCity, "Center");
/* 75 */ p3.add(p2, "East");
/* */
/* */
/* 78 */ JPanel p4 = new JPanel();
/* 79 */ p4.setLayout(new GridLayout(3, 1));
/* 80 */ p4.add(this.jtfName);
/* 81 */ p4.add(this.jtfStreet);
/* 82 */ p4.add(p3);
/* */
/* */
/* 85 */ JPanel jpAddress = new JPanel(new BorderLayout());
/* 86 */ jpAddress.add(p1, "West");
/* 87 */ jpAddress.add(p4, "Center");
/* */
/* */
/* 90 */ jpAddress.setBorder(new BevelBorder(0));
/* */
/* */
/* 93 */ JPanel jpButton = new JPanel();
/* 94 */ jpButton.add(this.jbtAdd);
/* 95 */ jpButton.add(this.jbtFirst);
/* 96 */ jpButton.add(this.jbtNext);
/* 97 */ jpButton.add(this.jbtPrevious);
/* 98 */ jpButton.add(this.jbtLast);
/* */
/* */
/* 101 */ add(jpAddress, "Center");
/* 102 */ add(jpButton, "South");
/* */
/* 104 */ this.jbtAdd.addActionListener(new ActionListener()
/* */ {
/* */ public void actionPerformed(ActionEvent e) {
/* 107 */ AddressBook.this.writeAddress();
/* */ }
/* */ });
/* 110 */ this.jbtFirst.addActionListener(new ActionListener()
/* */ {
/* */ public void actionPerformed(ActionEvent e) {
/* */ try {
/* 114 */ if (AddressBook.this.raf.length() > 0L) AddressBook.this.readAddress(0L);
/* */
/* 116 */ } catch (IOException ex) {
/* 117 */ ex.printStackTrace();
/* */ }
/* */ }
/* */ });
/* 121 */ this.jbtNext.addActionListener(new ActionListener()
/* */ {
/* */ public void actionPerformed(ActionEvent e) {
/* */ try {
/* 125 */ long currentPosition = AddressBook.this.raf.getFilePointer();
/* 126 */ if (currentPosition < AddressBook.this.raf.length()) {
/* 127 */ AddressBook.this.readAddress(currentPosition);
/* */ }
/* 129 */ } catch (IOException ex) {
/* 130 */ ex.printStackTrace();
/* */ }
/* */ }
/* */ });
/* 134 */ this.jbtPrevious.addActionListener(new ActionListener()
/* */ {
/* */ public void actionPerformed(ActionEvent e) {
/* */ try {
/* 138 */ long currentPosition = AddressBook.this.raf.getFilePointer();
/* 139 */ if (currentPosition - 182L > 0L) {
/* */
/* 141 */ AddressBook.this.readAddress(currentPosition - 364L);
/* */ } else {
/* 143 */ AddressBook.this.readAddress(0L);
/* */ }
/* 145 */ } catch (IOException ex) {
/* 146 */ ex.printStackTrace();
/* */ }
/* */ }
/* */ });
/* 150 */ this.jbtLast.addActionListener(new ActionListener() {
/* */ public void actionPerformed(ActionEvent e) {
/* */ try {
/* 153 */ long lastPosition = AddressBook.this.raf.length();
/* 154 */ if (lastPosition > 0L)
/* */ {
/* 156 */ AddressBook.this.readAddress(lastPosition - 182L);
/* */ }
/* 158 */ } catch (IOException ex) {
/* 159 */ ex.printStackTrace();
/* */ }
/* */ }
/* */ });
/* */
/* */
/* */ try {
/* 166 */ if (this.raf.length() > 0L) readAddress(0L);
/* */
/* 168 */ } catch (IOException ex) {
/* 169 */ ex.printStackTrace();
/* */ }
/* */ }
/* */
/* */
/* */ public void writeAddress() {
/* */ try {
/* 176 */ this.raf.seek(this.raf.length());
/* 177 */ FixedLengthStringIO.writeFixedLengthString(this.jtfName
/* 178 */ .getText(), 32, this.raf);
/* 179 */ FixedLengthStringIO.writeFixedLengthString(this.jtfStreet
/* 180 */ .getText(), 32, this.raf);
/* 181 */ FixedLengthStringIO.writeFixedLengthString(this.jtfCity
/* 182 */ .getText(), 20, this.raf);
/* 183 */ FixedLengthStringIO.writeFixedLengthString(this.jtfState
/* 184 */ .getText(), 2, this.raf);
/* 185 */ FixedLengthStringIO.writeFixedLengthString(this.jtfZip
/* 186 */ .getText(), 5, this.raf);
/* */ }
/* 188 */ catch (IOException ex) {
/* 189 */ ex.printStackTrace();
/* */ }
/* */ }
/* */
/* */
/* */ public void readAddress(long position) throws IOException {
/* 195 */ this.raf.seek(position);
/* 196 */ String name = FixedLengthStringIO.readFixedLengthString(32, this.raf);
/* */
/* 198 */ String street = FixedLengthStringIO.readFixedLengthString(32, this.raf);
/* */
/* 200 */ String city = FixedLengthStringIO.readFixedLengthString(20, this.raf);
/* */
/* 202 */ String state = FixedLengthStringIO.readFixedLengthString(2, this.raf);
/* */
/* 204 */ String zip = FixedLengthStringIO.readFixedLengthString(5, this.raf);
/* */
/* */
/* 207 */ this.jtfName.setText(name);
/* 208 */ this.jtfStreet.setText(street);
/* 209 */ this.jtfCity.setText(city);
/* 210 */ this.jtfState.setText(state);
/* 211 */ this.jtfZip.setText(zip);
/* */ }
/* */
/* */ public static void main(String[] args) {
/* 215 */ AddressBook frame = new AddressBook();
/* 216 */ frame.pack();
/* 217 */ frame.setTitle("AddressBook");
/* 218 */ frame.setDefaultCloseOperation(3);
/* 219 */ frame.setVisible(true);
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch17/AddressBook.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/