1: package j_201126100207;
2:
3: import java.awt.*;
4: import java.awt.event.*;
5: import static java.awt.event.InputEvent.*;
6: import java.io.BufferedReader;
7: import java.io.BufferedWriter;
8: import java.io.File;
9: import java.io.FileReader;
10: import java.io.FileWriter;
11: import java.io.IOException;
12: import javax.swing.*;
13: import javax.swing.event.*;
14: import javax.swing.filechooser.FileFilter;
15:
16: public class J_201126100207 extends JFrame {
17:
18: public final static String imagePath = "image/";
19: public final static Icon NEW24 = new ImageIcon(imagePath + "New24.gif", "new");
20: public final static Icon OPEN24 = new ImageIcon(imagePath + "Open24.gif", "open");
21: public final static Icon SAVE24 = new ImageIcon(imagePath + "Save24.gif", "save");
22: //内部成员;
23: private JFileChooser choose;
24: private File selectedFile = null;
25: private JTextArea textarea;
26: private boolean newPage = false;
27: private Font font;
28: //===========================================================================================================
29:
30: public J_201126100207(String title) {
31: Container content = getContentPane();
32: setTitle(title);
33: super.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
34:
35: JMenuBar mb = new JMenuBar();
36: setJMenuBar(mb);
37:
38: //菜单栏标题及内容设置
39: JMenu fileMenu = new JMenu("文件(F)");
40: mb.add(fileMenu);
41: fileMenu.setMnemonic('F');
42: JMenu javaTest = new JMenu("Java上机题目");
43: mb.add(javaTest);
44: JMenu communication = new JMenu("通讯录(C)");
45: mb.add(communication);
46: communication.setMnemonic('C');
47: JMenu help = new JMenu("帮助(H)");
48: mb.add(help);
49: help.setMnemonic('H');
50: //文件一栏的菜单
51: JMenuItem newItem = new JMenuItem("新建(N)");
52: fileMenu.add(newItem);
53: JMenuItem openItem = new JMenuItem("打开(O)...");
54: fileMenu.add(openItem);
55: JMenuItem saveItem = new JMenuItem("保存(S)");
56: fileMenu.add(saveItem);
57: fileMenu.addSeparator();
58: JMenuItem fontColorItem = new JMenuItem("字体与颜色(F)...");
59: fileMenu.add(fontColorItem);
60: fileMenu.addSeparator();
61: JMenuItem exitItem = new JMenuItem("退出(X)");
62: fileMenu.add(exitItem);
63: //Java上机题目一栏的菜单
64: JMenuItem repreatItem = new JMenuItem("回文数(H)...");
65: javaTest.add(repreatItem);
66: JMenuItem translationItem = new JMenuItem("数字与英语互译(T)...");
67: javaTest.add(translationItem);
68: JMenuItem countItem = new JMenuItem("统计英文数据(C)...");
69: javaTest.add(countItem);
70: JMenuItem sumItem = new JMenuItem("文本文件求和(M)...");
71: javaTest.add(sumItem);
72: //通讯录一栏的菜单
73: JMenuItem maintainItem = new JMenuItem("通讯维护(W)...");
74: communication.add(maintainItem);
75: communication.addSeparator();
76: JMenuItem setupItem = new JMenuItem("通讯录存储文件设置(L)...");
77: communication.add(setupItem);
78: JMenuItem aboutItem = new JMenuItem("关于(G)...");
79: help.add(aboutItem);
80:
81: //设置textArea:添加滚动条,设置默认字体,及背景颜色
82: textarea = new JTextArea();
83: JScrollPane scorl = new JScrollPane(textarea);
84: content.add(scorl);
85: //font= new Font("宋体", Font.PLAIN, 14);
86: textarea.setFont(font);
87: //toolbar组件定义
88: JToolBar toolBar = new JToolBar();
89: JButton newButton = new JButton(NEW24);
90: newButton.setToolTipText("新建文件");
91: JButton openButton = new JButton(OPEN24);
92: openButton.setToolTipText("打开文件");
93: JButton saveButton = new JButton(SAVE24);
94: saveButton.setToolTipText("保存文件");
95: toolBar.add(newButton);
96: toolBar.add(openButton);
97: toolBar.add(saveButton);
98: toolBar.setFloatable(false);
99:
100: JPanel pane = new JPanel();
101: BorderLayout bord = new BorderLayout();
102: pane.setLayout(bord);
103: pane.add("North", toolBar);
104: pane.add("Center", scorl);
105: content.add(pane);
106: //快捷键设置
107: newItem.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
108: openItem.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
109: saveItem.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
110: fontColorItem.setAccelerator(KeyStroke.getKeyStroke('F', CTRL_DOWN_MASK));
111: exitItem.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
112: repreatItem.setAccelerator(KeyStroke.getKeyStroke('H', CTRL_DOWN_MASK));
113: translationItem.setAccelerator(KeyStroke.getKeyStroke('T', CTRL_DOWN_MASK));
114: countItem.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
115: sumItem.setAccelerator(KeyStroke.getKeyStroke('M', CTRL_DOWN_MASK));
116: maintainItem.setAccelerator(KeyStroke.getKeyStroke('W', CTRL_DOWN_MASK));
117: setupItem.setAccelerator(KeyStroke.getKeyStroke('L', CTRL_DOWN_MASK));
118: aboutItem.setAccelerator(KeyStroke.getKeyStroke('G', CTRL_DOWN_MASK));
119:
120: //===================================================================================================================
121: //事件处理:
122: //窗口关闭
123: addWindowListener(new WindowAdapter() {
124: @Override
125: public void windowClosing(WindowEvent event) {
126: if (selectedFile == null && textarea.getText().equals("")) {
127: int retuenValue = JOptionPane.showConfirmDialog(null, " 确定要退出系统吗?", "是否退出", JOptionPane.YES_NO_OPTION);
128: if (retuenValue == JOptionPane.YES_OPTION) {
129: System.exit(0);
130: }
131: } else {
132: int btn = JOptionPane.showConfirmDialog(null, "是否保存到"
133: + selectedFile + "?", "是否保存",
134: JOptionPane.YES_NO_CANCEL_OPTION);
135: if (btn == JOptionPane.CANCEL_OPTION) {
136: } else if (btn == JOptionPane.NO_OPTION) {
137: System.exit(0);
138: } else {
139: if (selectedFile == null
140: && !textarea.getText().equals("")) {
141: choose = new JFileChooser();
142: int state = choose.showSaveDialog(J_201126100207.this);
143: if (state == JFileChooser.APPROVE_OPTION) {
144: try {
145: File file = choose.getSelectedFile();
146: BufferedWriter bw = new BufferedWriter(
147: new FileWriter(file));
148: String str = textarea.getText();
149: String[] lines = str.split("\n");
150: for (String line : lines) {
151: bw.write(line + "\r\n");
152: }
153: bw.flush();
154: file.createNewFile();
155: bw.close();
156: } catch (IOException e) {
157: JOptionPane.showConfirmDialog(null,
158: "保存文件失败!", "ERROR",
159: JOptionPane.ERROR_MESSAGE);
160: }
161: }
162: System.exit(0);
163: } else if (selectedFile != null) {
164: try {
165: BufferedWriter bw = new BufferedWriter(
166: new FileWriter(selectedFile));
167: String str = textarea.getText();
168: String[] lines = str.split("\n");
169: for (String line : lines) {
170: bw.write(line + "\r\n");
171: }
172: bw.flush();
173: bw.close();
174: } catch (IOException e) {
175: }
176: System.exit(0);
177: }
178: }
179: }
180: }
181: });
182:
183: //新建按钮
184: newItem.addActionListener(new ActionListener() {
185: @Override
186: public void actionPerformed(ActionEvent e) {
187: if (selectedFile == null && textarea.getText().equals("")) {
188: return;
189: } else {
190: int btn = JOptionPane.showConfirmDialog(J_201126100207.this, "是否保存到...?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION);
191: if (btn == JOptionPane.CANCEL_OPTION) {
192: return;
193: } else if (btn == JOptionPane.YES_OPTION) {
194: if (!textarea.getText().equals("")) {
195: JFileChooser choose = new JFileChooser();
196: int state = choose.showSaveDialog(J_201126100207.this);
197: if (state == JFileChooser.APPROVE_OPTION) {
198: try {
199: File file = choose.getSelectedFile();
200: BufferedWriter bw = new BufferedWriter(new FileWriter(file));
201: String str = textarea.getText();
202: String[] lines = str.split("\n");
203: for (String line : lines) {
204: bw.write(line + "\r\n");
205: }
206: bw.flush();
207: file.createNewFile();
208: bw.close();
209:
210: } catch (IOException ex) {
211: JOptionPane.showConfirmDialog(J_201126100207.this,
212: "保存文件失败!", "ERROR",
213: JOptionPane.ERROR_MESSAGE);
214: }
215: }
216: }
217: }
218: }
219: textarea.setText("");//清空textarea中的内容;
220:
221: }
222: });
223: newButton.addActionListener(new ActionListener() {
224: @Override
225: public void actionPerformed(ActionEvent e) {
226: if (selectedFile == null && textarea.getText().equals("")) {
227: return;
228: } else {
229: int btn = JOptionPane.showConfirmDialog(J_201126100207.this, "是否保存到...?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION);
230: if (btn == JOptionPane.CANCEL_OPTION) {
231: return;
232: } else if (btn == JOptionPane.YES_OPTION) {
233: if (!textarea.getText().equals("")) {
234: choose = new JFileChooser();
235: int state = choose.showSaveDialog(J_201126100207.this);
236: if (state == JFileChooser.APPROVE_OPTION) {
237: try {
238: File file = choose.getSelectedFile();
239: BufferedWriter bw = new BufferedWriter(new FileWriter(file));
240: String str = textarea.getText();
241: String[] lines = str.split("\n");
242: for (String line : lines) {
243: bw.write(line + "\r\n");
244: }
245: bw.flush();
246: file.createNewFile();
247: bw.close();
248: } catch (IOException ex) {
249: JOptionPane.showConfirmDialog(J_201126100207.this,
250: "保存文件失败!", "ERROR",
251: JOptionPane.ERROR_MESSAGE);
252: }
253: }
254: }
255: }
256: }
257: textarea.setText("");//清空textarea中的内容;
258: }
259: });
260:
261:
262: //打开按钮
263: openItem.addActionListener(new ActionListener() {
264: @Override
265: public void actionPerformed(ActionEvent e) {
266: if (selectedFile == null && textarea.getText().equals("")) {
267: } else {
268: int btn = JOptionPane.showConfirmDialog(J_201126100207.this,
269: "是否保存到...?", "是否保存",
270: JOptionPane.YES_NO_CANCEL_OPTION);
271: if (btn == JOptionPane.CANCEL_OPTION) {
272: return;
273: } else if (btn == JOptionPane.YES_OPTION) {
274: }
275: }
276: choose = new JFileChooser();
277: choose.addChoosableFileFilter(new fileFilter("java"));
278: choose.addChoosableFileFilter(new fileFilter("txt"));
279: choose.addChoosableFileFilter(new fileFilter("cpp"));
280: int state = choose.showOpenDialog(J_201126100207.this);
281: if (state == JFileChooser.APPROVE_OPTION) {
282: try {
283: selectedFile = choose.getSelectedFile();
284: BufferedReader br = new BufferedReader(new FileReader(selectedFile));
285: textarea.read(br, "");
286: br.close();
287: } catch (IOException ex) {
288: JOptionPane.showConfirmDialog(J_201126100207.this,
289: "打开文件失败", "error",
290: JOptionPane.ERROR_MESSAGE);
291: }
292: }
293: }
294: });
295:
296: //打开图标
297: openButton.addActionListener(new ActionListener() {
298: @Override
299: public void actionPerformed(ActionEvent e) {
300: if (selectedFile == null && textarea.getText().equals("")) {
301: } else {
302: int btn = JOptionPane.showConfirmDialog(J_201126100207.this,
303: "是否保存到...?", "是否保存",
304: JOptionPane.YES_NO_CANCEL_OPTION);
305: if (btn == JOptionPane.CANCEL_OPTION) {
306: return;
307: } else if (btn == JOptionPane.YES_OPTION) {
308: }
309: }
310: choose = new JFileChooser();
311: choose.addChoosableFileFilter(new fileFilter("java"));
312: choose.addChoosableFileFilter(new fileFilter("txt"));
313: choose.addChoosableFileFilter(new fileFilter("cpp"));
314: int state = choose.showOpenDialog(J_201126100207.this);
315: if (state == JFileChooser.APPROVE_OPTION) {
316: try {
317: selectedFile = choose.getSelectedFile();
318: BufferedReader br = new BufferedReader(new FileReader(selectedFile));
319: textarea.read(br, "");
320: br.close();
321: } catch (IOException ex) {
322: JOptionPane.showConfirmDialog(J_201126100207.this,
323: "打开文件失败", "error",
324: JOptionPane.ERROR_MESSAGE);
325: }
326: }
327: }
328: });
329:
330: //保存按钮
331: saveItem.addActionListener(new ActionListener() {
332: @Override
333: public void actionPerformed(ActionEvent event) {
334: if (newPage == true || selectedFile == null) {
335: choose = new JFileChooser();
336: int state = choose.showSaveDialog(J_201126100207.this);
337: if (state == JFileChooser.APPROVE_OPTION) {
338: try {
339: File file = choose.getSelectedFile();
340: BufferedWriter bw = new BufferedWriter(
341: new FileWriter(file));
342: String str = textarea.getText();
343: String[] lines = str.split("\n");
344: for (String line : lines) {
345: bw.write(line + "\r\n");
346: }
347: bw.flush();
348: file.createNewFile();
349: bw.close();
350: newPage = false;
351: selectedFile = file;
352: } catch (IOException e) {
353: JOptionPane.showMessageDialog(J_201126100207.this, "保存文件失败",
354: "ERROR", JOptionPane.ERROR_MESSAGE);
355: }
356: }
357: } else if (newPage == false && selectedFile != null) {
358:
359: try {
360: BufferedWriter bw = new BufferedWriter(new FileWriter(
361: selectedFile));
362: String str = textarea.getText();
363: String[] lines = str.split("\n");
364: for (String line : lines) {
365: bw.write(line + "\r\n");
366: }
367: bw.flush();
368: bw.close();
369: newPage = false;
370: } catch (IOException e) {
371: JOptionPane.showMessageDialog(J_201126100207.this, "保存文件失败",
372: "ERROR", JOptionPane.ERROR_MESSAGE);
373: }
374: }
375: }
376: });
377: //保存图标
378: saveButton.addActionListener(new ActionListener() {
379: @Override
380: public void actionPerformed(ActionEvent event) {
381: if (newPage == true || selectedFile == null) {
382: choose = new JFileChooser();
383: int state = choose.showSaveDialog(J_201126100207.this);
384: if (state == JFileChooser.APPROVE_OPTION) {
385: try {
386: File file = choose.getSelectedFile();
387: BufferedWriter bw = new BufferedWriter(
388: new FileWriter(file));
389: String str = textarea.getText();
390: String[] lines = str.split("\n");
391: for (String line : lines) {
392: bw.write(line + "\r\n");
393: }
394: bw.flush();
395: file.createNewFile();
396: bw.close();
397: newPage = false;
398: selectedFile = file;
399: } catch (IOException e) {
400: JOptionPane.showMessageDialog(J_201126100207.this, "保存文件失败",
401: "ERROR", JOptionPane.ERROR_MESSAGE);
402: }
403: }
404: } else if (newPage == false && selectedFile != null) {
405:
406: try {
407: BufferedWriter bw = new BufferedWriter(new FileWriter(
408: selectedFile));
409: String str = textarea.getText();
410: String[] lines = str.split("\n");
411: for (String line : lines) {
412: bw.write(line + "\r\n");
413: }
414: bw.flush();
415: bw.close();
416: newPage = false;
417: } catch (IOException e) {
418: JOptionPane.showMessageDialog(J_201126100207.this, "保存文件失败",
419: "ERROR", JOptionPane.ERROR_MESSAGE);
420: }
421: }
422: }
423: });
424: //字体颜色设置
425: fontColorItem.addActionListener(new ActionListener() {
426: @Override
427: public void actionPerformed(ActionEvent e) {
428: FontDialog fontdialog = new FontDialog(J_201126100207.this);
429:
430:
431: }
432: });
433:
434: exitItem.addActionListener(new ActionListener() {
435: @Override
436: public void actionPerformed(ActionEvent event) {
437: if (selectedFile == null && textarea.getText().equals("")) {
438: int retuenValue = JOptionPane.showConfirmDialog(null, " 确定要退出系统吗?", "是否退出", JOptionPane.YES_NO_OPTION);
439: if (retuenValue == JOptionPane.YES_OPTION) {
440: System.exit(0);
441: }
442: } else {
443: int btn = JOptionPane.showConfirmDialog(null, "是否保存到"
444: + selectedFile + "?", "是否保存",
445: JOptionPane.YES_NO_CANCEL_OPTION);
446: if (btn == JOptionPane.CANCEL_OPTION) {
447: return;
448: } else if (btn == JOptionPane.NO_OPTION) {
449: System.exit(0);
450: } else {
451: if (selectedFile == null
452: && !textarea.getText().equals("")) {
453: choose = new JFileChooser();
454: int state = choose.showSaveDialog(J_201126100207.this);
455: if (state == JFileChooser.APPROVE_OPTION) {
456: try {
457: File file = choose.getSelectedFile();
458: BufferedWriter bw = new BufferedWriter(
459: new FileWriter(file));
460: String str = textarea.getText();
461: String[] lines = str.split("\n");
462: for (String line : lines) {
463: bw.write(line + "\r\n");
464: }
465: bw.flush();
466: file.createNewFile();
467: bw.close();
468: } catch (IOException e) {
469: JOptionPane.showConfirmDialog(null,
470: "保存文件失败!", "ERROR",
471: JOptionPane.ERROR_MESSAGE);
472: }
473: }
474: System.exit(0);
475: } else if (selectedFile != null) {
476: try {
477: BufferedWriter bw = new BufferedWriter(
478: new FileWriter(selectedFile));
479: String str = textarea.getText();
480: String[] lines = str.split("\n");
481: for (String line : lines) {
482: bw.write(line + "\r\n");
483: }
484: bw.flush();
485: bw.close();
486: } catch (IOException e) {
487: }
488: System.exit(0);
489: }
490: }
491: }
492: }
493: });
494:
495: repreatItem.addActionListener(new ActionListener() {
496: @Override
497: public void actionPerformed(ActionEvent event) {
498: repreatDialog repreat = new repreatDialog(J_201126100207.this);
499: }
500: });
501:
502: translationItem.addActionListener(new ActionListener() {
503: @Override
504: public void actionPerformed(ActionEvent event) {
505: translateDialog translation = new translateDialog(J_201126100207.this);
506: }
507: });
508:
509:
510: countItem.addActionListener(new ActionListener() {
511: @Override
512: public void actionPerformed(ActionEvent event) {
513: countDialog CountDialog = new countDialog(J_201126100207.this);
514:
515: }
516: });
517:
518: sumItem.addActionListener(new ActionListener() {
519: @Override
520: public void actionPerformed(ActionEvent event) {
521: sumDialog sum = new sumDialog(J_201126100207.this);
522: }
523: });
524:
525: //关于按钮
526: aboutItem.addActionListener(new ActionListener() {
527: @Override
528: public void actionPerformed(ActionEvent e) {
529: JOptionPane.showMessageDialog(J_201126100207.this, "Java记事本" + "\n"
530: + "designed by 姜楠\n" + " in ZJUT", "关于",
531: JOptionPane.INFORMATION_MESSAGE);
532: }
533: });
534:
535: }
536: //================================================================================================================
537:
538: /**
539: * 内部类定义以及实现
540: */
541: private class FontDialog extends JDialog implements ActionListener, ItemListener {
542:
543: private JButton fontColorButton;
544: private JButton backGroundButton;
545: private JButton okButton;
546: private JButton cancelButton;
547: private String fontName;
548: private int fontSize;
549: private Font font;
550: private JComboBox nameCombo;
551: private JComboBox sizeCombo;
552: private JLabel fontDisplay;
553:
554: public FontDialog(J_201126100207 window) {
555: GridLayout grid = new GridLayout(6, 2);
556: setLayout(grid);
557: setModal(true);
558: setTitle("设置颜色与字体");
559:
560: font = textarea.getFont();
561: fontName = font.getFontName();
562: fontSize = font.getSize();
563:
564: //获取所有的字体
565: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
566: String[] fontNames = ge.getAvailableFontFamilyNames();
567: nameCombo = new JComboBox(fontNames);
568: nameCombo.addItemListener(this);
569:
570: //创建和添加组件到FontDialog
571: JLabel nameLabel = new JLabel("字体名称");
572: add(nameLabel);
573: add(nameCombo);
574:
575: JLabel sizeLabel = new JLabel("字体大小");
576: String[] size = {"8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32", "34", "42", "52", "62", "72"};
577: sizeCombo = new JComboBox(size);
578: sizeCombo.addItemListener(this);
579: add(sizeLabel);
580: add(sizeCombo);
581:
582: JLabel fontLabel = new JLabel("字体颜色");
583: fontColorButton = new JButton("设置...");
584: fontColorButton.addActionListener(this);
585: add(fontLabel);
586: add(fontColorButton);
587: JLabel backGroundLabel = new JLabel("背景颜色");
588: backGroundButton = new JButton("设置...");
589: backGroundButton.addActionListener(this);
590: add(backGroundLabel);
591: add(backGroundButton);
592: JLabel displayLabel = new JLabel("字体预览");
593: fontDisplay = new JLabel("Java 字体预览");
594: fontDisplay.setFont(font);
595: JPanel displayPanel = new JPanel(true);
596: displayPanel.setBackground(Color.WHITE);
597: displayPanel.add(fontDisplay);
598: add(displayLabel);
599:
600: add(displayPanel);
601: okButton = new JButton("确定");
602: okButton.addActionListener(this);
603: cancelButton = new JButton("取消");
604: cancelButton.addActionListener(this);
605: add(okButton);
606: add(cancelButton);
607: setSize(600, 300);
608: setLocationRelativeTo(window);
609: setVisible(true);
610: }
611:
612: @Override
613: public void itemStateChanged(ItemEvent e) {
614: if (e.getStateChange() == ItemEvent.SELECTED) {
615: if (e.getSource() == nameCombo) {
616: fontName = (String) nameCombo.getSelectedItem();
617: } else if (e.getSource() == sizeCombo) {
618: fontSize = Integer.parseInt(sizeCombo.getSelectedItem().toString());
619: }
620: font = new Font(fontName, font.PLAIN, fontSize);
621: fontDisplay.setFont(font);
622: fontDisplay.repaint();
623: }
624: }
625:
626: @Override
627: public void actionPerformed(ActionEvent e) {
628: if (e.getSource() == fontColorButton) {
629: Color color = JColorChooser.showDialog(null, "选择字体颜色", getBackground());
630: textarea.setForeground(color);
631: } else if (e.getSource() == backGroundButton) {
632: Color color = JColorChooser.showDialog(null, "选择背景颜色", getBackground());
633: textarea.setForeground(color);
634: } else if (e.getSource() == okButton) {
635: textarea.setFont(font); // Set the selected font
636: setVisible(false);
637: } else if (e.getSource() == cancelButton) {
638: setVisible(false);
639: }
640: }
641: }
642:
643: //================================================================================================================
644: private class fileFilter extends FileFilter {
645:
646: String ext;
647:
648: public fileFilter(String ext) {
649: this.ext = ext;
650: }
651:
652: @Override
653: public boolean accept(File file) {
654: String name = file.getName().toLowerCase();
655: if (name.endsWith(ext) || file.isDirectory()) {
656: return true;
657: } else {
658: return false;
659: }
660: }
661:
662: @Override
663: public String getDescription() {
664: if (ext.equals("java")) {
665: return "*.java";
666: } else if (ext.equals("txt")) {
667: return "*.txt";
668: } else if (ext.equals("cpp")) {
669: return "*.cpp";
670: }
671: return "";
672: }
673: }
674: //================================================================================================================
675:
676: private class countDialog extends JDialog implements ActionListener {
677:
678: JLabel label1, label2, label3;
679: JTextField textField1, textField2, textField3;
680: JButton okButton;
681: JButton cancelButton;
682:
683: public countDialog(J_201126100207 window) {
684: GridLayout grid = new GridLayout(4, 2);
685: setLayout(grid);
686: setModal(true);
687: setTitle("统计英文数据");
688:
689: label1 = new JLabel("以字母w 开头的单词数");
690: add(label1);
691: textField1 = new JTextField();
692: textField1.setEditable(false);
693: add(textField1);
694: label2 = new JLabel("含'or'字符串的单词数");
695: add(label2);
696: textField2 = new JTextField();
697: textField2.setEditable(false);
698: add(textField2);
699: label3 = new JLabel("长度为 3 的单词数");
700: add(label3);
701: textField3 = new JTextField();
702: textField3.setEditable(false);
703: add(textField3);
704: JPanel okPanel = new JPanel();
705: okButton = new JButton("确定");
706: okPanel.add(okButton);
707: okButton.addActionListener(this);
708: this.add(okPanel);
709: JPanel cancelPanel = new JPanel();
710: cancelButton = new JButton("退出");
711: cancelPanel.add(cancelButton);
712: cancelButton.addActionListener(this);
713: this.add(cancelPanel);
714:
715: int beginWithW = 0, includeOr = 0, lengthEqualToThree = 0;
716: String str = textarea.getText();
717: if (!str.equals("")) {
718: String[] lines = str.split("\n");
719: if (!lines.equals("")) {
720: for (String line : lines) {
721: String t[] = line.split(" ");
722: for (String temp : t) {
723: if (temp.charAt(0) == 'W' || temp.charAt(0) == 'w') {
724: beginWithW++;
725: }
726: if (temp.indexOf("or") != -1) {
727: includeOr++;
728: }
729: if (temp.length() == 3) {
730: lengthEqualToThree++;
731: }
1: }
2: }
3: }
4: textField1.setText(String.valueOf(beginWithW));
5: textField2.setText(String.valueOf(includeOr));
6: textField3.setText(String.valueOf(lengthEqualToThree));
7: setSize(400, 200);
8: setLocationRelativeTo(window);
9: setVisible(true);
10: }
11:
12: @Override
13: public void actionPerformed(ActionEvent e) {
14: if (e.getSource() == okButton || e.getSource() == cancelButton) {
15: this.setVisible(false);
16: }
17: }
18: }
19:
20: private class translateDialog extends JDialog implements ActionListener {
21:
22: JLabel cinLabel, coutLabel;
23: JTextField cinTextField, coutTextField;
24: JButton okButton;
25: JButton cancelButton;
26: String x[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
27: String y[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
28: String z[] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
29:
30: public translateDialog(J_201126100207 window) {
31: GridLayout grid = new GridLayout(3, 2);
32: setLayout(grid);
33: setModal(true);
34: setTitle("数字与英文互译");
35:
36: cinLabel = new JLabel("请输入一个不大于100的数字或英文:");
37: add(cinLabel);
38: cinTextField = new JTextField();
39: add(cinTextField);
40: coutLabel = new JLabel("显示翻译结果:");
41: add(coutLabel);
42: coutTextField = new JTextField();
43: coutTextField.setEditable(false);
44: add(coutTextField);
45:
46: JPanel okPanel = new JPanel();
47: okButton = new JButton("确定");
48: okPanel.add(okButton);
49: add(okPanel);
50: okButton.addActionListener(this);
51: JPanel cancelPanel = new JPanel();
52: cancelButton = new JButton("取消");
53: cancelPanel.add(cancelButton);
54: add(cancelPanel);
55: cancelButton.addActionListener(this);
56:
57: setSize(500, 150);
58: setLocationRelativeTo(window);
59: setVisible(true);
60: }
61:
62: @Override
63: public void actionPerformed(ActionEvent e) {
64: if (e.getSource() == okButton) {
65: String str = cinTextField.getText();
66: String trim = str.trim();
67: if (trim.equals("")) {
68: JOptionPane.showMessageDialog(translateDialog.this, "未按要求输入,请重新输入",
69: "错误", JOptionPane.ERROR_MESSAGE);
70: } else {
71: if (trim.charAt(0) <= '9' && trim.charAt(0) >= '0') {
72: int temp = Integer.parseInt(trim);
73: String s = null;
74: if (temp <= 9) {
75: s = x[temp];
76: }
77: if (temp >= 10 && temp <= 19) {
78: s = y[temp - 10];
79: } else if(temp>=20 && temp<=99){
80: s = z[temp / 10 - 2];
81: if (temp % 10 != 0) {
82: s += " " + x[temp % 10];
83: }
84: }
85: coutTextField.setText(s);
86: }
87: else {
88: int answer = 0;
89: String[] part = trim.split(" ");
90: for (int i = 0; i < part.length; i++) {
91: if (!part[i].equals("")) {
92: for (int j = 0; j < x.length; j++) {
93: if(part[i].equals(x[j])){
94: answer+=j+1;
95: }
96: }
97: for (int j = 0; j < y.length; j++) {
98: if(part[i].equals(y[j])){
99: answer+=j+10;
100: }
101: }
102: for (int j = 0; j < z.length; j++) {
103: if(part[i].equals(z[j])){
104: answer+=(j+2)*10;
105: }
106: }
107: }
108: }
109: coutTextField.setText(String.valueOf(answer));
110: }
111:
112: }
113: } else if (e.getSource() == cancelButton) {
114: this.setVisible(false);
115: }
116:
117: }
118: }
119:
120: private class repreatDialog extends JDialog implements ActionListener {
121:
122: JLabel cinLabel;
123: JTextField cinTextField;
124: JButton judgeButton;
125: JButton cancelButton;
126:
127: public repreatDialog(J_201126100207 window) {
128: GridLayout grid = new GridLayout(2, 2);
129: setLayout(grid);
130: setModal(true);
131: setTitle("回文数");
132:
133: cinLabel = new JLabel("输入1-99999之间的整数:");
134: add(cinLabel);
135: cinTextField = new JTextField();
136: add(cinTextField);
137: JPanel judgePanel = new JPanel();
138: judgeButton = new JButton("判断是否是回文数?");
139: judgePanel.add(judgeButton);
140: add(judgePanel);
141: judgeButton.addActionListener(this);
142: JPanel cancelPanel = new JPanel();
143: cancelButton = new JButton("取消");
144: cancelPanel.add(cancelButton);
145: add(cancelPanel);
146: cancelButton.addActionListener(this);
147:
148: setSize(400, 100);
149: setLocationRelativeTo(window);
150: setVisible(true);
151: }
152:
153: private boolean isLegal(String s) {
154: if (s.length() > 5) {
155: return false;
156: } else {
157: for (int i = 0; i < s.length(); i++) {
158: if (s.charAt(i) > '9' || s.charAt(i) < '0') {
159: return false;
160: }
161: }
162: }
163: return true;
164: }
165:
166: private boolean isReverse(String s) {
167: for (int i = 0; i < s.length(); i++) {
168: if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
169: return false;
170: }
171: }
172: return true;
173: }
174:
175: @Override
176: public void actionPerformed(ActionEvent e) {
177: if (e.getSource() == judgeButton) {
178: String str = cinTextField.getText();
179: if (str.equals("")) {
180: JOptionPane.showMessageDialog(repreatDialog.this, "未按要求输入,请重新输入",
181: "错误", JOptionPane.ERROR_MESSAGE);
182: } else if (isLegal(str)) {
183: if (isReverse(str)) {
184: JOptionPane.showMessageDialog(repreatDialog.this, str+"是回文数",
185: "确认", JOptionPane.INFORMATION_MESSAGE);
186: } else {
187: JOptionPane.showMessageDialog(repreatDialog.this, str+"不是回文数",
188: "确认", JOptionPane.INFORMATION_MESSAGE);
189: }
190: }
191: cinTextField.setText("");
192:
193: } else if (e.getSource() == cancelButton) {
194: this.setVisible(false);
195: }
196: }
197: }
198:
199: private class sumDialog extends JDialog implements ActionListener, ChangeListener {
200:
201: JProgressBar progressBar;
202: JLabel sumLabel;
203: Timer timer;
204: JButton okButton;
205: int values = 0, number = 0, total = 0;
206: String[] lines, temp;
207:
208: public sumDialog(J_201126100207 window) {
209: GridLayout grid = new GridLayout(3, 1);
210: setLayout(grid);
211: setModal(true);
212: setTitle("文本文件求和");
213:
214: sumLabel = new JLabel("", JLabel.CENTER);
215: this.add(sumLabel);
216: progressBar = new JProgressBar();
217: progressBar.setOrientation(JProgressBar.HORIZONTAL);
218: progressBar.setMinimum(0);
219: progressBar.setMaximum(100);
220: progressBar.setValue(0);
221: progressBar.setStringPainted(true);
222: progressBar.addChangeListener(this);
223: progressBar.setPreferredSize(new Dimension(200, 30));
224: progressBar.setBorderPainted(true);
225: this.add(progressBar);
226: timer = new Timer(5, this);
227: JPanel okPanel = new JPanel();
228: okButton = new JButton("开始计算");
229: okPanel.add(okButton);
230: add(okPanel);
231: okButton.addActionListener(this);
232: setSize(400, 200);
233: setLocationRelativeTo(window);
234: setVisible(true);
235: }
236:
237: @Override
238: public void actionPerformed(ActionEvent e) {
239: if (e.getSource() == okButton) {
240: timer.start();
241: number = 0;
242: values = 0;
243: String str = textarea.getText();
244: lines = str.split("\n");
245: for (int i = 0; i < lines.length; i++) {
246: temp = lines[i].split(" ");
247: for (int j = 0; j < temp.length; j++) {
248: total++;
249: }
250: }
251: for (int i = 0; i < lines.length; i++) {
252: for (int j = 0; j < temp.length; j++, number++) {
253: if (!temp[j].equals("")) {
254: values += Integer.parseInt(temp[j]);
255: }
256: }
257: }
258: } else if (e.getSource() == timer) {
259: int value = progressBar.getValue();
260: if (value < 100) {
261: value = (int) 100 * number / total;
262: progressBar.setValue(value);
263: } else {
264: timer.stop();
265: JOptionPane.showMessageDialog(sumDialog.this, Integer.toString(values),
266: "确认", JOptionPane.INFORMATION_MESSAGE);
267: progressBar.setValue(0);
268: }
269: }
270: }
271:
272: @Override
273: public void stateChanged(ChangeEvent e1) {
274: int value = progressBar.getValue();
275: if (e1.getSource() == progressBar) {
276: sumLabel.setText("目前已完成进度:" + Integer.toString(value) + "%");
277: }
278: }
279: }
280: //================================================================================================================
281:
282: //窗口居中函数
283: public static void centerWindow(Window f) {
284: Toolkit tk = f.getToolkit();
285: Dimension dm = tk.getScreenSize();
286: f.setLocation((int) (dm.getWidth() - f.getWidth()) / 2, (int) (dm.getHeight() - f.getHeight()) / 2);
287: }
288:
289: public static void main(String[] args) {
290: JFrame fr = new J_201126100207("201126100207-姜楠-Java 程序设计综合实验");
291: fr.setSize(1000, 500);
292: centerWindow(fr);
293: fr.setVisible(true);
294: }
295: }