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 AwtDemo4.{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 WindowAdapter25.{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有适配器MouseAdapter45. 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,有适配器KeyAdapter63. 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有适配器MouseAdapter49. 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. else99. {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.java4.打jar包:jar -cvf my.jar mymenu5.带配置信息:jar -cvfm my.jar 1.txt mymenu6.*/7.package mymenu8.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. try78. {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. try110. {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.
效果图