博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GUI
阅读量:6711 次
发布时间:2019-06-25

本文共 30162 字,大约阅读时间需要 100 分钟。

 

GUI

  • java.Awt:需要调用本地系统方法实现功能,属于重量级控件 ;
  • javax.Swing:在Awt的基础上建立的一套图形界面系统,提供了更多的控件,而且完全由Java实现,增强了移植性,属于轻量级控件 ;

继承关系图

 


常见的布局管理器

FlowLayout(流式布局管理器):从左到右顺序排列,Panel默认的布局管理器;

BorderLayout(边界布局管理器):东南西北中的布局方式,Frame默认的布局管理器;
GridLayout(网格布局管理器):把窗体画成网格的布局管理器;
GridBagLayout(网格包布局管理器):非规则的矩阵;
CardLayout(卡片布局管理器):选项卡;

创建图形化界面

1.创建frame窗体;

2.对窗体进行设置,包括大小,位置等等;
3.定义组件,通过add方法添加到窗体中;
4.让窗体显示.setVisible(true)

事件监听机制的特点

1.事件源:就是awt或者swing包中的那些图形界面组件;

2.事件:每一个事件源都有自己特有的对象事件和共性事件;
3.监听器:将可以触发某一个事件的动作(不止一个)都已经封装到了监听器中;
以上三者在Java中都已经定义好了,直接获取对象来用就可以了


1.import java.awt.*; 2.import java.awt.event.*; 3.class  AwtDemo 4.{
5. public static void main(String[] args) 6. {
7. Frame f = new Frame("my awt"); 8. f.setSize(500,400); 9. f.setLocation(300,200); 10. f.setLayout(new FlowLayout()); 11. Button b = new Button("登录"); 12. f.add(b); 13. f.setVisible(true); 14. //添加一个监听器 15. f.addWindowListener(new MyWinListener()); 16. } 17. 18.} 19./* 20.WindowListener是一个接口,如果要调用里面的方法,得把里面的方法全部覆盖,所以用它的子类WindowAdapter, 21.WindowAdapter是一个抽象类,不能实例化,所以继承它并复写我们需要的方法 22.*/ 23.//窗体事件 24.class MyWinListener extends WindowAdapter 25.{
26. public void windowClosing(WindowEvent e) 27. {
28. System.out.println("我关了,"+e.toString()); 29. System.exit(0); 30. } 31. public void windowActivated(WindowEvent e) 32. {
33. System.out.println("被激活了"); 34. } 35. public void windowOpened(WindowEvent e) 36. {
37. System.out.println("我被打开了"); 38. } 39. 40.}

窗体事件,Action事件,鼠标事件,键盘事件

1.import java.awt.*; 2.import java.awt.event.*; 3.class FrameDemo 4.{
5. private Frame fr; 6. private Button bu; 7. private TextField te; 8. FrameDemo() 9. {
10. init(); 11. } 12. public void init() 13. {
14. fr = new Frame("my awt"); 15. te = new TextField(20); 16. bu = new Button("my button"); 17. fr.setBounds(500,400,500,400); 18. fr.setLayout(new FlowLayout()); 19. fr.add(te); 20. fr.add(bu); 21. myEvent(); 22. fr.setVisible(true); 23. } 24. public void myEvent() 25. {
26. //窗体事件 27. fr.addWindowListener(new WindowAdapter() 28. {
29. public void windowClosing(WindowEvent e) 30. {
31. System.out.println("我关了"); 32. System.exit(0); 33. } 34. }); 35. //Action事件 36. //ActionListener是为数不多的没有适配器的监听器 37. bu.addActionListener(new ActionListener() 38. {
39. public void actionPerformed(ActionEvent e) 40. {
41. System.out.println("活动了"); 42. } 43. }); 44. //鼠标事件,MouseListener有适配器MouseAdapter 45. bu.addMouseListener(new MouseAdapter() 46. {
47. int clickcount = 1; 48. int entercount = 1; 49. public void mouseClicked(MouseEvent e) 50. {
51. //System.out.println("点击"+clickcount++); 52. if (e.getClickCount()==2 ) 53. {
54. System.out.println("双击了"); 55. } 56. } 57. public void mouseEntered(MouseEvent e) 58. {
59. System.out.println("进入"+entercount++); 60. } 61. }); 62. //键盘事件,KeyListener,有适配器KeyAdapter 63. bu.addKeyListener(new KeyAdapter() 64. {
65. public void keyPressed(KeyEvent e) 66. {
67. if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER) 68. {
69. System.out.println("ctrl enter run"); 70. } 71. System.out.println(KeyEvent.getKeyText(e.getKeyCode())+" ... "+e.getKeyCode()); 72. } 73. }); 74. te.addKeyListener(new KeyAdapter() 75. {
76. public void keyPressed(KeyEvent e) 77. {
78. int code = e.getKeyCode(); 79. //在文本框中输入数字,字母不让进,且提示非法 80. if (!(code>=KeyEvent.VK_NUMPAD0 && code<=KeyEvent.VK_NUMPAD9)) 81. {
82. e.consume();//void consume(): 使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。 83. System.out.println("非法数据"); 84. } 85. } 86. }); 87. } 88. public static void main(String[] args) 89. {
90. FrameDemo f = new FrameDemo(); 91. } 92.}

GUI练习

在文本框输入目录,点击”转到”按钮,将该目录中的文件与文件夹名称列在下面的文本区域中

1.import java.awt.*; 2.import java.awt.event.*; 3.import java.io.*; 4.class FrameDemo2 5.{
6. private Frame fr; 7. private Button bu; 8. private TextField te; 9. private TextArea ta; 10. private Dialog di; 11. private Label la; 12. private Button okbut; 13. FrameDemo2() 14. {
15. init(); 16. } 17. public void init() 18. {
19. fr = new Frame("my awt");//创建一个窗体 20. te = new TextField(40); 21. bu = new Button("转到"); 22. ta = new TextArea(30,50); 23. di = new Dialog(fr,"信息提示",true);//创建一个对话窗 24. la = new Label(); 25. okbut = new Button("确定");//创建对话窗的按钮 26. fr.setBounds(500,400,700,500); 27. fr.setLayout(new FlowLayout()); 28. di.setBounds(600,500,300,200);//设置对话框的位置,大小 29. di.setLayout(new FlowLayout());//设置对话框的排列的方式 30. fr.add(te); 31. fr.add(bu); 32. fr.add(ta); 33. di.add(la);//将标签加入对话框 34. di.add(okbut);//将按钮加入对话框 35. myEvent(); 36. fr.setVisible(true); 37. } 38. public void myEvent() 39. {
40. //窗体事件 41. fr.addWindowListener(new WindowAdapter() 42. {
43. public void windowClosing(WindowEvent e) 44. {
45. System.exit(0); 46. } 47. }); 48. //鼠标事件,MouseListener有适配器MouseAdapter 49. bu.addMouseListener(new MouseAdapter() 50. {
51. public void mouseClicked(MouseEvent e) 52. {
53. show(); 54. } 55. }); 56. di.addWindowListener(new WindowAdapter() 57. {
58. public void windowClosing(WindowEvent e) 59. {
60. di.setVisible(false); 61. } 62. }); 63. okbut.addMouseListener(new MouseAdapter() 64. {
65. public void mouseClicked(MouseEvent e) 66. {
67. di.setVisible(false); 68. } 69. }); 70. //直接用Enter键就可以直接来代替上面按钮的功能 71. te.addKeyListener(new KeyAdapter() 72. {
73. public void keyPressed(KeyEvent e) 74. {
75. if (e.getKeyCode()==KeyEvent.VK_ENTER) 76. {
77. show(); 78. } 79. } 80. }); 81. 82. } 83. //将查找文件的方法封装到一个方法里,以方便调用 84. public void show() 85. {
86. String dir = te.getText(); 87. te.setText(""); 88. File f = new File(dir); 89. if (f.exists() && f.isDirectory()) 90. {
91. ta.setText(""); 92. String[] arr = f.list(); 93. for (String str : arr) 94. {
95. ta.append(str+"\r\n"); 96. } 97. } 98. else 99. {
100. String msg = "输入的地址:"+dir+"有误,请重新输入"; 101. la.setText(msg); 102. di.setVisible(true); 103. } 104. } 105. public static void main(String[] args) 106. {
107. new FrameDemo2(); 108. } 109.} 110.

效果图

 

菜单栏的应用

1.//菜单栏的应用 2./* 3.带包名编译:javac -d e:\\1 MyMenuDemo.java 4.打jar包:jar -cvf my.jar mymenu 5.带配置信息:jar -cvfm my.jar 1.txt mymenu 6.*/ 7.package mymenu 8.import java.awt.*; 9.import java.awt.event.*; 10.import java.io.*; 11.public class MyMenuDemo 12.{
13. private Frame fr; 14. private MenuBar mb; 15. private Menu me,subme; 16. private MenuItem closemi,openmi,savemi,submi; 17. private FileDialog loaddia,savedia; 18. private TextArea ta; 19. private File file; 20. MyMenuDemo() 21. {
22. init(); 23. myEvent(); 24. fr.setVisible(true); 25. } 26. private void init() 27. {
28. fr = new Frame("my awt"); 29. //创建菜单栏 30. mb = new MenuBar(); 31. //创建菜单 32. me = new Menu("文件"); 33. subme = new Menu("子菜单"); 34. closemi = new MenuItem("退出"); 35. savemi = new MenuItem("保存"); 36. openmi = new MenuItem("打开"); 37. submi = new MenuItem("子条目"); 38. loaddia = new FileDialog(fr,"打开",FileDialog.LOAD);//创建一个文件对话框 39. savedia = new FileDialog(fr,"保存",FileDialog.SAVE); 40. ta = new TextArea(); 41. 42. fr.setBounds(400,500,500,400); 43. fr.setMenuBar(mb);//在窗体中加入菜单栏 44. fr.add(ta); 45. 46. mb.add(me);//菜单栏中加入"文件"菜单 47. 48. me.add(subme);//"文件"菜单中加入"子菜单" 49. me.add(openmi);//"文件"菜单中加入"打开"条目 50. me.add(savemi); 51. me.add(closemi); 52. 53. subme.add(submi);//"子菜单"中加入"子条目" 54. } 55. private void myEvent() 56. {
57. fr.addWindowListener(new WindowAdapter() 58. {
59. public void windowClosing(WindowEvent e) 60. {
61. System.exit(0); 62. } 63. }); 64. openmi.addActionListener(new ActionListener() 65. {
66. public void actionPerformed(ActionEvent e) 67. {
68. loaddia.setVisible(true); 69. String dir = loaddia.getDirectory(); 70. String fi = loaddia.getFile(); 71. if (dir==null || fi==null) 72. {
73. return; 74. } 75. ta.setText(""); 76. File file = new File(dir,fi); 77. try 78. {
79. BufferedReader bufr = new BufferedReader(new FileReader(file)); 80. String line = null; 81. while ((line=bufr.readLine())!=null) 82. {
83. ta.append(line+"\r\n"); 84. } 85. bufr.close(); 86. } 87. catch (IOException ex) 88. {
89. throw new RuntimeException("打开失败"); 90. } 91. } 92. }); 93. savemi.addActionListener(new ActionListener() 94. {
95. public void actionPerformed(ActionEvent e) 96. {
97. //这里如果文件已经存在了,就直接保存,不用弹出对话框 98. if (file==null) 99. {
100. savedia.setVisible(true); 101. String dir = savedia.getDirectory(); 102. String fi = savedia.getFile(); 103. if (dir == null || fi == null) 104. {
105. return; 106. } 107. file = new File(dir,fi); 108. } 109. try 110. {
111. BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); 112. String str = ta.getText(); 113. bufw.write(str); 114. bufw.flush(); 115. bufw.close(); 116. } 117. catch (IOException ex) 118. {
119. throw new RuntimeException("保存失败"); 120. } 121. } 122. 123. }); 124. closemi.addActionListener(new ActionListener() 125. {
126. public void actionPerformed(ActionEvent e) 127. {
128. System.exit(0); 129. } 130. }); 131. } 132. public static void main(String[] args) 133. {
134. new MyMenuDemo(); 135. } 136.} 137.

效果图

 

@%2802.%u8D44%u6599%29%5BGUI%2C%20awt%5D%0A%23GUI%0A%3E-%20java.Awt%3A%u9700%u8981%u8C03%u7528%u672C%u5730%u7CFB%u7EDF%u65B9%u6CD5%u5B9E%u73B0%u529F%u80FD%2C%u5C5E%u4E8E%u91CD%u91CF%u7EA7%u63A7%u4EF6%20%3B%0A%3E-%20javax.Swing%3A%u5728Awt%u7684%u57FA%u7840%u4E0A%u5EFA%u7ACB%u7684%u4E00%u5957%u56FE%u5F62%u754C%u9762%u7CFB%u7EDF%2C%u63D0%u4F9B%u4E86%u66F4%u591A%u7684%u63A7%u4EF6%2C%u800C%u4E14%u5B8C%u5168%u7531Java%u5B9E%u73B0%2C%u589E%u5F3A%u4E86%u79FB%u690D%u6027%2C%u5C5E%u4E8E%u8F7B%u91CF%u7EA7%u63A7%u4EF6%20%3B%0A%0A%23%23%u7EE7%u627F%u5173%u7CFB%u56FE%0A%21%5BAlt%20text%5D%28./GUI.jpg%29%0A%0A---%0A%0A%23%23%u5E38%u89C1%u7684%u5E03%u5C40%u7BA1%u7406%u5668%0A%3EFlowLayout%28%u6D41%u5F0F%u5E03%u5C40%u7BA1%u7406%u5668%29%3A%u4ECE%u5DE6%u5230%u53F3%u987A%u5E8F%u6392%u5217%2CPanel%u9ED8%u8BA4%u7684%u5E03%u5C40%u7BA1%u7406%u5668%3B%0A%3EBorderLayout%28%u8FB9%u754C%u5E03%u5C40%u7BA1%u7406%u5668%29%3A%u4E1C%u5357%u897F%u5317%u4E2D%u7684%u5E03%u5C40%u65B9%u5F0F%2CFrame%u9ED8%u8BA4%u7684%u5E03%u5C40%u7BA1%u7406%u5668%3B%0A%3EGridLayout%28%u7F51%u683C%u5E03%u5C40%u7BA1%u7406%u5668%29%3A%u628A%u7A97%u4F53%u753B%u6210%u7F51%u683C%u7684%u5E03%u5C40%u7BA1%u7406%u5668%3B%0A%3EGridBagLayout%28%u7F51%u683C%u5305%u5E03%u5C40%u7BA1%u7406%u5668%29%3A%u975E%u89C4%u5219%u7684%u77E9%u9635%3B%0A%3ECardLayout%28%u5361%u7247%u5E03%u5C40%u7BA1%u7406%u5668%29%3A%u9009%u9879%u5361%3B%0A%0A%0A%23%23%u521B%u5EFA%u56FE%u5F62%u5316%u754C%u9762%0A%3E1.%u521B%u5EFAframe%u7A97%u4F53%3B%0A%3E2.%u5BF9%u7A97%u4F53%u8FDB%u884C%u8BBE%u7F6E%2C%u5305%u62EC%u5927%u5C0F%2C%u4F4D%u7F6E%u7B49%u7B49%3B%0A%3E3.%u5B9A%u4E49%u7EC4%u4EF6%2C%u901A%u8FC7add%u65B9%u6CD5%u6DFB%u52A0%u5230%u7A97%u4F53%u4E2D%3B%0A%3E4.%u8BA9%u7A97%u4F53%u663E%u793A.setVisible%28true%29%0A%0A%23%23%u4E8B%u4EF6%u76D1%u542C%u673A%u5236%u7684%u7279%u70B9%0A%3E1.%u4E8B%u4EF6%u6E90%3A%u5C31%u662Fawt%u6216%u8005swing%u5305%u4E2D%u7684%u90A3%u4E9B%u56FE%u5F62%u754C%u9762%u7EC4%u4EF6%3B%0A%3E2.%u4E8B%u4EF6%3A%u6BCF%u4E00%u4E2A%u4E8B%u4EF6%u6E90%u90FD%u6709%u81EA%u5DF1%u7279%u6709%u7684%u5BF9%u8C61%u4E8B%u4EF6%u548C%u5171%u6027%u4E8B%u4EF6%3B%0A%3E3.%u76D1%u542C%u5668%3A%u5C06%u53EF%u4EE5%u89E6%u53D1%u67D0%u4E00%u4E2A%u4E8B%u4EF6%u7684%u52A8%u4F5C%28%u4E0D%u6B62%u4E00%u4E2A%29%u90FD%u5DF2%u7ECF%u5C01%u88C5%u5230%u4E86%u76D1%u542C%u5668%u4E2D%3B%0A%3E%u4EE5%u4E0A%u4E09%u8005%u5728Java%u4E2D%u90FD%u5DF2%u7ECF%u5B9A%u4E49%u597D%u4E86%2C%u76F4%u63A5%u83B7%u53D6%u5BF9%u8C61%u6765%u7528%u5C31%u53EF%u4EE5%u4E86%0A%0A---%0A%60%60%60java%0Aimport%20java.awt.*%3B%0Aimport%20java.awt.event.*%3B%0Aclass%20%20AwtDemo%0A%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%0A%09%7B%0A%09%09Frame%20f%20%3D%20new%20Frame%28%22my%20awt%22%29%3B%0A%09%09f.setSize%28500%2C400%29%3B%0A%09%09f.setLocation%28300%2C200%29%3B%0A%09%09f.setLayout%28new%20FlowLayout%28%29%29%3B%0A%09%09Button%20b%20%3D%20new%20Button%28%22%u767B%u5F55%22%29%3B%0A%09%09f.add%28b%29%3B%0A%09%09f.setVisible%28true%29%3B%0A%09%09//%u6DFB%u52A0%u4E00%u4E2A%u76D1%u542C%u5668%0A%09%09f.addWindowListener%28new%20MyWinListener%28%29%29%3B%0A%09%7D%0A%0A%7D%0A/*%0AWindowListener%u662F%u4E00%u4E2A%u63A5%u53E3%2C%u5982%u679C%u8981%u8C03%u7528%u91CC%u9762%u7684%u65B9%u6CD5%2C%u5F97%u628A%u91CC%u9762%u7684%u65B9%u6CD5%u5168%u90E8%u8986%u76D6%2C%u6240%u4EE5%u7528%u5B83%u7684%u5B50%u7C7BWindowAdapter%2C%0AWindowAdapter%u662F%u4E00%u4E2A%u62BD%u8C61%u7C7B%2C%u4E0D%u80FD%u5B9E%u4F8B%u5316%2C%u6240%u4EE5%u7EE7%u627F%u5B83%u5E76%u590D%u5199%u6211%u4EEC%u9700%u8981%u7684%u65B9%u6CD5%0A*/%0A//%u7A97%u4F53%u4E8B%u4EF6%0Aclass%20MyWinListener%20extends%20WindowAdapter%0A%7B%0A%09public%20void%20windowClosing%28WindowEvent%20e%29%0A%09%7B%0A%09%09System.out.println%28%22%u6211%u5173%u4E86%2C%22+e.toString%28%29%29%3B%0A%09%09System.exit%280%29%3B%0A%09%7D%0A%09public%20void%20windowActivated%28WindowEvent%20e%29%20%0A%09%7B%0A%09%09System.out.println%28%22%u88AB%u6FC0%u6D3B%u4E86%22%29%3B%0A%09%7D%0A%09public%20void%20windowOpened%28WindowEvent%20e%29%20%0A%09%7B%0A%09%09System.out.println%28%22%u6211%u88AB%u6253%u5F00%u4E86%22%29%3B%0A%09%7D%0A%0A%7D%0A%60%60%60%0A%0A---%0A**%u7A97%u4F53%u4E8B%u4EF6%2CAction%u4E8B%u4EF6%2C%u9F20%u6807%u4E8B%u4EF6%2C%u952E%u76D8%u4E8B%u4EF6**%0A%60%60%60java%0Aimport%20java.awt.*%3B%0Aimport%20java.awt.event.*%3B%0Aclass%20FrameDemo%20%0A%7B%0A%09private%20Frame%20fr%3B%0A%09private%20Button%20bu%3B%0A%09private%20TextField%20te%3B%0A%09FrameDemo%28%29%0A%09%7B%0A%09%09init%28%29%3B%0A%09%7D%0A%09public%20void%20init%28%29%0A%09%7B%0A%09%09fr%20%3D%20new%20Frame%28%22my%20awt%22%29%3B%09%09%0A%09%09te%20%3D%20new%20TextField%2820%29%3B%0A%09%09bu%20%3D%20new%20Button%28%22my%20button%22%29%3B%0A%09%09fr.setBounds%28500%2C400%2C500%2C400%29%3B%0A%09%09fr.setLayout%28new%20FlowLayout%28%29%29%3B%0A%09%09fr.add%28te%29%3B%0A%09%09fr.add%28bu%29%3B%0A%09%09myEvent%28%29%3B%0A%09%09fr.setVisible%28true%29%3B%0A%09%7D%0A%09public%20void%20myEvent%28%29%0A%09%7B%0A%09%09//%u7A97%u4F53%u4E8B%u4EF6%0A%09%09fr.addWindowListener%28new%20WindowAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20windowClosing%28WindowEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.out.println%28%22%u6211%u5173%u4E86%22%29%3B%0A%09%09%09%09System.exit%280%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09//Action%u4E8B%u4EF6%0A%09%09//ActionListener%u662F%u4E3A%u6570%u4E0D%u591A%u7684%u6CA1%u6709%u9002%u914D%u5668%u7684%u76D1%u542C%u5668%0A%09%09bu.addActionListener%28new%20ActionListener%28%29%0A%09%09%7B%0A%09%09%09public%20void%20actionPerformed%28ActionEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.out.println%28%22%u6D3B%u52A8%u4E86%22%29%3B%0A%09%09%09%7D%09%0A%09%09%7D%29%3B%0A%09%09//%u9F20%u6807%u4E8B%u4EF6%2CMouseListener%u6709%u9002%u914D%u5668MouseAdapter%0A%09%09bu.addMouseListener%28new%20MouseAdapter%28%29%0A%09%09%7B%0A%09%09%09int%20clickcount%20%3D%201%3B%0A%09%09%09int%20entercount%20%3D%201%3B%0A%09%09%09public%20void%20mouseClicked%28MouseEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09//System.out.println%28%22%u70B9%u51FB%22+clickcount++%29%3B%0A%09%09%09%09if%20%28e.getClickCount%28%29%3D%3D2%20%29%0A%09%09%09%09%7B%0A%09%09%09%09%09System.out.println%28%22%u53CC%u51FB%u4E86%22%29%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09public%20void%20mouseEntered%28MouseEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.out.println%28%22%u8FDB%u5165%22+entercount++%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09//%u952E%u76D8%u4E8B%u4EF6%2CKeyListener%2C%u6709%u9002%u914D%u5668KeyAdapter%0A%09%09bu.addKeyListener%28new%20KeyAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20keyPressed%28KeyEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09if%28e.isControlDown%28%29%20%26%26%20e.getKeyCode%28%29%3D%3DKeyEvent.VK_ENTER%29%0A%09%09%09%09%7B%0A%09%09%09%09%09System.out.println%28%22ctrl%20enter%20run%22%29%3B%09%0A%09%09%09%09%7D%0A%09%09%09%09System.out.println%28KeyEvent.getKeyText%28e.getKeyCode%28%29%29+%22%20%20...%20%20%22+e.getKeyCode%28%29%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09te.addKeyListener%28new%20KeyAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20keyPressed%28KeyEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09int%20code%20%3D%20e.getKeyCode%28%29%3B%0A%09%09%09%09//%u5728%u6587%u672C%u6846%u4E2D%u8F93%u5165%u6570%u5B57%2C%u5B57%u6BCD%u4E0D%u8BA9%u8FDB%2C%u4E14%u63D0%u793A%u975E%u6CD5%0A%09%09%09%09if%20%28%21%28code%3E%3DKeyEvent.VK_NUMPAD0%20%26%26%20code%3C%3DKeyEvent.VK_NUMPAD9%29%29%0A%09%09%09%09%7B%0A%09%09%09%09%09e.consume%28%29%3B//void%20consume%28%29%3A%20%u4F7F%u7528%u6B64%u4E8B%u4EF6%uFF0C%u4EE5%u4FBF%u4E0D%u4F1A%u6309%u7167%u9ED8%u8BA4%u7684%u65B9%u5F0F%u7531%u4EA7%u751F%u6B64%u4E8B%u4EF6%u7684%u6E90%u4EE3%u7801%u6765%u5904%u7406%u6B64%u4E8B%u4EF6%u3002%20%0A%09%09%09%09%09System.out.println%28%22%u975E%u6CD5%u6570%u636E%22%29%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%0A%09%7B%0A%09%09FrameDemo%20f%20%3D%20new%20FrameDemo%28%29%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%0A---%0A%0A**GUI%u7EC3%u4E60**%0A%3E%u5728%u6587%u672C%u6846%u8F93%u5165%u76EE%u5F55%2C%u70B9%u51FB%22%u8F6C%u5230%22%u6309%u94AE%2C%u5C06%u8BE5%u76EE%u5F55%u4E2D%u7684%u6587%u4EF6%u4E0E%u6587%u4EF6%u5939%u540D%u79F0%u5217%u5728%u4E0B%u9762%u7684%u6587%u672C%u533A%u57DF%u4E2D%0A%0A%60%60%60java%0Aimport%20java.awt.*%3B%0Aimport%20java.awt.event.*%3B%0Aimport%20java.io.*%3B%0Aclass%20FrameDemo2%20%0A%7B%0A%09private%20Frame%20fr%3B%0A%09private%20Button%20bu%3B%0A%09private%20TextField%20te%3B%0A%09private%20TextArea%20ta%3B%0A%09private%20Dialog%20di%3B%0A%09private%20Label%20la%3B%0A%09private%20Button%20okbut%3B%0A%09FrameDemo2%28%29%0A%09%7B%0A%09%09init%28%29%3B%0A%09%7D%0A%09public%20void%20init%28%29%0A%09%7B%0A%09%09fr%20%3D%20new%20Frame%28%22my%20awt%22%29%3B//%u521B%u5EFA%u4E00%u4E2A%u7A97%u4F53%09%09%0A%09%09te%20%3D%20new%20TextField%2840%29%3B%0A%09%09bu%20%3D%20new%20Button%28%22%u8F6C%u5230%22%29%3B%0A%09%09ta%20%3D%20new%20TextArea%2830%2C50%29%3B%0A%09%09di%20%3D%20new%20Dialog%28fr%2C%22%u4FE1%u606F%u63D0%u793A%22%2Ctrue%29%3B//%u521B%u5EFA%u4E00%u4E2A%u5BF9%u8BDD%u7A97%0A%09%09la%20%3D%20new%20Label%28%29%3B%0A%09%09okbut%20%3D%20new%20Button%28%22%u786E%u5B9A%22%29%3B//%u521B%u5EFA%u5BF9%u8BDD%u7A97%u7684%u6309%u94AE%0A%09%09fr.setBounds%28500%2C400%2C700%2C500%29%3B%0A%09%09fr.setLayout%28new%20FlowLayout%28%29%29%3B%0A%09%09di.setBounds%28600%2C500%2C300%2C200%29%3B//%u8BBE%u7F6E%u5BF9%u8BDD%u6846%u7684%u4F4D%u7F6E%2C%u5927%u5C0F%0A%09%09di.setLayout%28new%20FlowLayout%28%29%29%3B//%u8BBE%u7F6E%u5BF9%u8BDD%u6846%u7684%u6392%u5217%u7684%u65B9%u5F0F%0A%09%09fr.add%28te%29%3B%0A%09%09fr.add%28bu%29%3B%0A%09%09fr.add%28ta%29%3B%0A%09%09di.add%28la%29%3B//%u5C06%u6807%u7B7E%u52A0%u5165%u5BF9%u8BDD%u6846%0A%09%09di.add%28okbut%29%3B//%u5C06%u6309%u94AE%u52A0%u5165%u5BF9%u8BDD%u6846%0A%09%09myEvent%28%29%3B%0A%09%09fr.setVisible%28true%29%3B%0A%09%7D%0A%09public%20void%20myEvent%28%29%0A%09%7B%0A%09%09//%u7A97%u4F53%u4E8B%u4EF6%0A%09%09fr.addWindowListener%28new%20WindowAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20windowClosing%28WindowEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.exit%280%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09//%u9F20%u6807%u4E8B%u4EF6%2CMouseListener%u6709%u9002%u914D%u5668MouseAdapter%0A%09%09bu.addMouseListener%28new%20MouseAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20mouseClicked%28MouseEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09show%28%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09di.addWindowListener%28new%20WindowAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20windowClosing%28WindowEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09di.setVisible%28false%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09okbut.addMouseListener%28new%20MouseAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20mouseClicked%28MouseEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09di.setVisible%28false%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09//%u76F4%u63A5%u7528Enter%u952E%u5C31%u53EF%u4EE5%u76F4%u63A5%u6765%u4EE3%u66FF%u4E0A%u9762%u6309%u94AE%u7684%u529F%u80FD%0A%09%09te.addKeyListener%28new%20KeyAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20keyPressed%28KeyEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09if%20%28e.getKeyCode%28%29%3D%3DKeyEvent.VK_ENTER%29%0A%09%09%09%09%7B%0A%09%09%09%09%09show%28%29%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%0A%09%7D%0A%09//%u5C06%u67E5%u627E%u6587%u4EF6%u7684%u65B9%u6CD5%u5C01%u88C5%u5230%u4E00%u4E2A%u65B9%u6CD5%u91CC%2C%u4EE5%u65B9%u4FBF%u8C03%u7528%0A%09public%20void%20show%28%29%0A%09%7B%0A%09%09String%20dir%20%3D%20te.getText%28%29%3B%0A%09%09te.setText%28%22%22%29%3B%09%09%0A%09%09File%20f%20%3D%20new%20File%28dir%29%3B%0A%09%09if%20%28f.exists%28%29%20%26%26%20f.isDirectory%28%29%29%0A%09%09%7B%0A%09%09%09ta.setText%28%22%22%29%3B%0A%09%09%09String%5B%5D%20arr%20%3D%20f.list%28%29%3B%0A%09%09%09for%20%28String%20str%20%3A%20arr%29%0A%09%09%09%7B%0A%09%09%09%09ta.append%28str+%22%5Cr%5Cn%22%29%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09String%20msg%20%3D%20%22%u8F93%u5165%u7684%u5730%u5740%3A%22+dir+%22%u6709%u8BEF%2C%u8BF7%u91CD%u65B0%u8F93%u5165%22%3B%0A%09%09%09la.setText%28msg%29%3B%0A%09%09%09di.setVisible%28true%29%3B%0A%09%09%7D%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%0A%09%7B%0A%09%09new%20FrameDemo2%28%29%3B%0A%09%7D%0A%7D%0A%0A%60%60%60%0A---%0A**%u6548%u679C%u56FE**%0A%21%5BAlt%20text%5D%28./1.jpg%29%0A%0A%0A%23%23%u83DC%u5355%u680F%u7684%u5E94%u7528%0A%60%60%60java%0A//%u83DC%u5355%u680F%u7684%u5E94%u7528%0A/*%0A%u5E26%u5305%u540D%u7F16%u8BD1%3Ajavac%20-d%20e%3A%5C%5C1%20MyMenuDemo.java%0A%u6253jar%u5305%3Ajar%20-cvf%20my.jar%20mymenu%0A%u5E26%u914D%u7F6E%u4FE1%u606F%3Ajar%20-cvfm%20my.jar%201.txt%20mymenu%0A*/%0Apackage%20mymenu%0Aimport%20java.awt.*%3B%0Aimport%20java.awt.event.*%3B%0Aimport%20java.io.*%3B%0Apublic%20class%20MyMenuDemo%20%0A%7B%0A%09private%20Frame%20fr%3B%0A%09private%20MenuBar%20mb%3B%0A%09private%20Menu%20me%2Csubme%3B%0A%09private%20MenuItem%20closemi%2Copenmi%2Csavemi%2Csubmi%3B%0A%09private%20FileDialog%20loaddia%2Csavedia%3B%0A%09private%20TextArea%20ta%3B%0A%09private%20File%20file%3B%0A%09MyMenuDemo%28%29%0A%09%7B%0A%09%09init%28%29%3B%0A%09%09myEvent%28%29%3B%0A%09%09fr.setVisible%28true%29%3B%0A%09%7D%0A%09private%20void%20init%28%29%0A%09%7B%0A%09%09fr%20%3D%20new%20Frame%28%22my%20awt%22%29%3B%0A%09%09//%u521B%u5EFA%u83DC%u5355%u680F%0A%09%09mb%20%3D%20new%20MenuBar%28%29%3B%0A%09%09//%u521B%u5EFA%u83DC%u5355%0A%09%09me%20%3D%20new%20Menu%28%22%u6587%u4EF6%22%29%3B%0A%09%09subme%20%3D%20new%20Menu%28%22%u5B50%u83DC%u5355%22%29%3B%0A%09%09closemi%20%3D%20new%20MenuItem%28%22%u9000%u51FA%22%29%3B%0A%09%09savemi%20%3D%20new%20MenuItem%28%22%u4FDD%u5B58%22%29%3B%0A%09%09openmi%20%3D%20new%20MenuItem%28%22%u6253%u5F00%22%29%3B%0A%09%09submi%20%3D%20new%20MenuItem%28%22%u5B50%u6761%u76EE%22%29%3B%0A%09%09loaddia%20%3D%20new%20FileDialog%28fr%2C%22%u6253%u5F00%22%2CFileDialog.LOAD%29%3B//%u521B%u5EFA%u4E00%u4E2A%u6587%u4EF6%u5BF9%u8BDD%u6846%0A%09%09savedia%20%3D%20new%20FileDialog%28fr%2C%22%u4FDD%u5B58%22%2CFileDialog.SAVE%29%3B%0A%09%09ta%20%3D%20new%20TextArea%28%29%3B%0A%0A%09%09fr.setBounds%28400%2C500%2C500%2C400%29%3B%09%09%0A%09%09fr.setMenuBar%28mb%29%3B//%u5728%u7A97%u4F53%u4E2D%u52A0%u5165%u83DC%u5355%u680F%0A%09%09fr.add%28ta%29%3B%0A%09%09%0A%09%09mb.add%28me%29%3B//%u83DC%u5355%u680F%u4E2D%u52A0%u5165%22%u6587%u4EF6%22%u83DC%u5355%0A%09%09%0A%09%09me.add%28subme%29%3B//%22%u6587%u4EF6%22%u83DC%u5355%u4E2D%u52A0%u5165%22%u5B50%u83DC%u5355%22%0A%09%09me.add%28openmi%29%3B//%22%u6587%u4EF6%22%u83DC%u5355%u4E2D%u52A0%u5165%22%u6253%u5F00%22%u6761%u76EE%0A%09%09me.add%28savemi%29%3B%0A%09%09me.add%28closemi%29%3B%0A%09%09%0A%09%09subme.add%28submi%29%3B//%22%u5B50%u83DC%u5355%22%u4E2D%u52A0%u5165%22%u5B50%u6761%u76EE%22%0A%09%7D%0A%09private%20void%20myEvent%28%29%0A%09%7B%0A%09%09fr.addWindowListener%28new%20WindowAdapter%28%29%0A%09%09%7B%0A%09%09%09public%20void%20windowClosing%28WindowEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.exit%280%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09openmi.addActionListener%28new%20ActionListener%28%29%0A%09%09%7B%0A%09%09%09public%20void%20actionPerformed%28ActionEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09loaddia.setVisible%28true%29%3B%0A%09%09%09%09String%20dir%20%3D%20loaddia.getDirectory%28%29%3B%0A%09%09%09%09String%20fi%20%3D%20loaddia.getFile%28%29%3B%0A%09%09%09%09if%20%28dir%3D%3Dnull%20%7C%7C%20fi%3D%3Dnull%29%0A%09%09%09%09%7B%0A%09%09%09%09%09return%3B%0A%09%09%09%09%7D%0A%09%09%09%09ta.setText%28%22%22%29%3B%0A%09%09%09%09File%20file%20%3D%20new%20File%28dir%2Cfi%29%3B%0A%09%09%09%09try%0A%09%09%09%09%7B%0A%09%09%09%09%09BufferedReader%20bufr%20%3D%20new%20BufferedReader%28new%20FileReader%28file%29%29%3B%0A%09%09%09%09%09String%20line%20%3D%20null%3B%0A%09%09%09%09%09while%20%28%28line%3Dbufr.readLine%28%29%29%21%3Dnull%29%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09ta.append%28line+%22%5Cr%5Cn%22%29%3B%0A%09%09%09%09%09%7D%0A%09%09%09%09%09bufr.close%28%29%3B%0A%09%09%09%09%7D%0A%09%09%09%09catch%20%28IOException%20ex%29%0A%09%09%09%09%7B%0A%09%09%09%09%09throw%20new%20RuntimeException%28%22%u6253%u5F00%u5931%u8D25%22%29%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%09savemi.addActionListener%28new%20ActionListener%28%29%0A%09%09%7B%0A%09%09%09public%20void%20actionPerformed%28ActionEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09//%u8FD9%u91CC%u5982%u679C%u6587%u4EF6%u5DF2%u7ECF%u5B58%u5728%u4E86%2C%u5C31%u76F4%u63A5%u4FDD%u5B58%2C%u4E0D%u7528%u5F39%u51FA%u5BF9%u8BDD%u6846%0A%09%09%09%09if%20%28file%3D%3Dnull%29%0A%09%09%09%09%7B%0A%09%09%09%09%09savedia.setVisible%28true%29%3B%0A%09%09%09%09%09String%20dir%20%3D%20savedia.getDirectory%28%29%3B%0A%09%09%09%09%09String%20fi%20%3D%20savedia.getFile%28%29%3B%0A%09%09%09%09%09if%20%28dir%20%3D%3D%20null%20%7C%7C%20fi%20%3D%3D%20null%29%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09return%3B%0A%09%09%09%09%09%7D%0A%09%09%09%09%09file%20%3D%20new%20File%28dir%2Cfi%29%3B%0A%09%09%09%09%7D%0A%09%09%09%09try%0A%09%09%09%09%7B%0A%09%09%09%09%09BufferedWriter%20bufw%20%3D%20new%20BufferedWriter%28new%20FileWriter%28file%29%29%3B%0A%09%09%09%09%09String%20str%20%3D%20ta.getText%28%29%3B%0A%09%09%09%09%09bufw.write%28str%29%3B%0A%09%09%09%09%09bufw.flush%28%29%3B%0A%09%09%09%09%09bufw.close%28%29%3B%0A%09%09%09%09%7D%0A%09%09%09%09catch%20%28IOException%20ex%29%0A%09%09%09%09%7B%0A%09%09%09%09%09throw%20new%20RuntimeException%28%22%u4FDD%u5B58%u5931%u8D25%22%29%3B%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%0A%09%09%7D%29%3B%0A%09%09closemi.addActionListener%28new%20ActionListener%28%29%0A%09%09%7B%0A%09%09%09public%20void%20actionPerformed%28ActionEvent%20e%29%0A%09%09%09%7B%0A%09%09%09%09System.exit%280%29%3B%0A%09%09%09%7D%0A%09%09%7D%29%3B%0A%09%7D%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%0A%09%7B%0A%09%09new%20MyMenuDemo%28%29%3B%0A%09%7D%0A%7D%0A%0A%60%60%60%0A---%0A**%u6548%u679C%u56FE**%0A%0A%21%5BAlt%20text%5D%28./2.jpg%29%0A

 

转载于:https://www.cnblogs.com/RuntimExcep/p/7121308.html

你可能感兴趣的文章
大数据助推新型智库建设
查看>>
新加坡欲重组通信和媒体管制机构
查看>>
《CCNP ROUTE 300-101学习指南》——2.2节构建EIGRP拓扑表
查看>>
《为自己工作——世界顶级设计师成功法则》目录—导读
查看>>
Libreboot 项目向开源社区示好和致歉
查看>>
《CMYK 2.0——设计师色彩管理手册》—第1章1.3节副作用
查看>>
java.util.concurrent.locks.LockSupport
查看>>
红帽加入 Node.js 基金会白金会员
查看>>
《OpenGL编程指南》一2.7 独立的着色器对象
查看>>
Ionic 3.4.2 发布,漂亮的 HTML5 移动应用框架
查看>>
Linux Kernel 4.9-rc8,4.9 分支最后一个候选版
查看>>
想开发 Android 分支?没门!
查看>>
《Web异步与实时交互——iframe AJAX WebSocket开发实战》—— 2.2 相关关键技术及工作原理...
查看>>
《Nmap渗透测试指南》—第1章1.5节Mac OS安
查看>>
重磅,企业实施大数据的路径
查看>>
linux之cp/scp命令+scp命令详解
查看>>
Spark 源码分析 -- BlockStore
查看>>
《C语言编程初学者指南》一1.7 创建并运行第一个C程序
查看>>
学习和使用 PHP 应该注意的10件事
查看>>
《Ember.js实战》——2.5 Ember.js对象模型
查看>>