曹杨教学点? 北桥教学点? 学校总部? | 设为首页 | 加入收藏 | 官方微信
365棋牌新手卡怎么使用_365棋牌全民游戏_365棋牌游客服充值
打造“培训+就业”一站式服务品牌!
  • Python+AI工程师
  • Web前端工程师
  • Java软件开发工程师
banner1

首页>>学员天地

java中Frame框架关闭按钮和框架居中的设置方法
编辑:eamol? 阅读:2017次? 发布:16-02-24
  1. import?java.awt.Button;??
  2. import?java.awt.Dimension;??
  3. import?java.awt.FlowLayout;??
  4. import?java.awt.Frame;??
  5. import?java.awt.TextField;??
  6. import?java.awt.Toolkit;??
  7. import?java.awt.event.WindowEvent;??
  8. import?java.awt.event.WindowListener;??
  9. ??
  10. //让框架居中显示及Frame框架关闭按钮的功能实现??
  11. public?class?MyFrame?extends?Frame?implements?WindowListener?{??
  12. ??
  13. ????public?MyFrame(){??
  14. ????????super("this?is?frame");??//设置窗口标题??
  15. ????????setSize(400,?300);???//设置窗口尺寸??
  16. ????????//获取屏幕的高度和宽度??
  17. ????????int?w=this.getWidth();??//获取框架的宽度??
  18. ????????int?h=this.getHeight();???//获取框架的高度??
  19. ????????Dimension?screenSize=Toolkit.getDefaultToolkit().getScreenSize();???????
  20. ????????double?screenWidth=screenSize.getWidth();??//获取屏幕的宽度??
  21. ????????double?screenHeith=screenSize.getHeight();??//获取屏幕的高度??
  22. ??????????
  23. ????????int?x=(int)(screenWidth-w)/2;??????
  24. ????????int?y=(int)(screenHeith-h)/2;??
  25. ??????????
  26. ????????//设置框架显示的位置??
  27. ????????setLocation(x,?y);??
  28. ??????????
  29. //??????this.setBounds((int)((screenWidth)/2-this.getWidth()),(int)((screenHeith/2)-this.getHeight()),?200,?200);??//?100,100分别为距离x,y轴的距离,后面的x,y分别框架的长和宽??
  30. ????????this.setLayout(new?FlowLayout());?????//设置布局管理器为流布局管理器??
  31. ????????this.add(new?TextField("hello?welocme?to?java!"));??
  32. ????????this.addWindowListener(this);???//添加窗口监听器??
  33. ????????this.setVisible(true);?????????//设置窗口可见???true可见,false?不可见??
  34. ????}??
  35. ????public?static?void?main(String[]?args)?{??
  36. ????????new?MyFrame();???//实例化??
  37. ????}??
  38. ??
  39. ????public?void?windowOpened(WindowEvent?e)?{}??
  40. ??????
  41. ????public?void?windowClosing(WindowEvent?e)???
  42. ????????{??
  43. ??????????????e.getWindow().dispose();??
  44. ????????}??//关闭窗口方法??
  45. ??????
  46. ????public?void?windowClosed(WindowEvent?e)?{}??
  47. ??????
  48. ????public?void?windowIconified(WindowEvent?e)?{}??
  49. ??????
  50. ????public?void?windowDeiconified(WindowEvent?e)?{}??
  51. ??
  52. ????public?void?windowActivated(WindowEvent?e)?{}??
  53. ??
  54. ????public?void?windowDeactivated(WindowEvent?e)?{}??
  55. ??
  56. }??


上一篇:?jscript和javascript的区别? ?下一篇:?java中JFrame的背景色和背景图片的设置
网络大专、自考本科,学历轻松拿!
?2016-2020 上海内威职业技能培训学校 版权所有
普陀区真南路1051弄5号303室(邮编:200331)
咨询:021-62434966? 合作:021-13371929650? 就业:021-62434966? 传真:021-? 电邮:service@snetway.com
《中华人民共和国电信与信息服务业务》经营许可证沪ICP备15002602号?