黑马程序员_学习笔记第22天——图形化界面
2013-12-27 00:56
453 查看
----------------------
ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------
1、GUI图形用户界面(Graphical User Interface)
2、java.Awt :Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属重量级控件。
3、javax.Swing :在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现,增强了移植性,属轻量级控件。
4、Container:为容器,是一个特殊的组建,该组件中可以通过add方法添加其他组件进来。
Component:组件,构件
1)Container:
-->Window: 窗口
-->Frame:窗体
-->Dialog: 对话框
-->FileDialog 文本对话框
-->Panel:面板
2)Button:按钮
3)Label:标签(封装文字)
4)Checkbox:复选框
5)TextComponent:文本组件
-->TextArea
-->TextField
5、布局管理器:
FlowLayout(流式布局管理器):从左到右的顺序排列,Panel默认的布局管理器
BorderLayout(边界布局管理器):东南西北中,Frame默认的布局管理器(需指明位置,不然会全屏覆盖,新添加的会覆盖先前的)
GridLayout(网格布局管理器):规则的矩阵
CardLayout(卡片布局管理器):选项卡
GridBagLayout(网格包布局管理器):非规则的矩阵
6、创建图形化界面:
1)创建frame窗体
2)对窗体进行基本设置
比如大小,位置,布局
3)定义组件
4)将组件通过窗体的add方法添加到窗体中
5)让窗体显示,通过setVisible(true)
7、事件监听机制组成:
事件源(组件):就是awt包或者swing包中的那些图形界面组件。
事件(Event):每一个事件源都有自己特有的对应事件和共性事件。
监听器(Listener):将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
注:以上三者,在java中都已经定义好了,直接获取其对象来用就可以了。
事件处理(引发事件后处理方式):对产生的动作进行处理。(实际开发中所要做的)
8、练习1
9、练习2
----------------------
ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------
1、GUI图形用户界面(Graphical User Interface)
2、java.Awt :Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属重量级控件。
3、javax.Swing :在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现,增强了移植性,属轻量级控件。
4、Container:为容器,是一个特殊的组建,该组件中可以通过add方法添加其他组件进来。
Component:组件,构件
1)Container:
-->Window: 窗口
-->Frame:窗体
-->Dialog: 对话框
-->FileDialog 文本对话框
-->Panel:面板
2)Button:按钮
3)Label:标签(封装文字)
4)Checkbox:复选框
5)TextComponent:文本组件
-->TextArea
-->TextField
5、布局管理器:
FlowLayout(流式布局管理器):从左到右的顺序排列,Panel默认的布局管理器
BorderLayout(边界布局管理器):东南西北中,Frame默认的布局管理器(需指明位置,不然会全屏覆盖,新添加的会覆盖先前的)
GridLayout(网格布局管理器):规则的矩阵
CardLayout(卡片布局管理器):选项卡
GridBagLayout(网格包布局管理器):非规则的矩阵
6、创建图形化界面:
1)创建frame窗体
2)对窗体进行基本设置
比如大小,位置,布局
3)定义组件
4)将组件通过窗体的add方法添加到窗体中
5)让窗体显示,通过setVisible(true)
public class AwtDemo1 { public static void main(String[] args) { Frame f = new Frame("my awt"); f.setSize(500,400); f.setLocation(300,200); f.setLayout(new FlowLayout()); Button b = new Button("我是一个按钮"); f.add(b); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.out.println("我关"); System.exit(0); } public void windowActivated(WindowEvent e){ System.out.println("我活了"); } public void windowOpened(WindowEvent e){ System.out.println("我被打开了。。。。"); } }); f.setVisible(true); } }
7、事件监听机制组成:
事件源(组件):就是awt包或者swing包中的那些图形界面组件。
事件(Event):每一个事件源都有自己特有的对应事件和共性事件。
监听器(Listener):将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
注:以上三者,在java中都已经定义好了,直接获取其对象来用就可以了。
事件处理(引发事件后处理方式):对产生的动作进行处理。(实际开发中所要做的)
public class FrameDemo1 { private Frame f ; private Button but; FrameDemo1() { init(); } public void init() { f = new Frame("my frame"); //对frame进行基本设置 f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); but = new Button("my button"); //将组件添加到frame中 f.add(but); //加载一下窗体上事件 myEvent(); //显示窗体 f.setVisible(true); } private void myEvent() { f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("退出,按钮干的"); System.exit(0); } }); } public static void main(String[] args) { new FrameDemo1(); } }
public class MouseAndKeyEventDemo1 { private Frame f ; private Button but; private TextField tf; MouseAndKeyEventDemo1() { init(); } public void init() { f = new Frame("my frame"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf = new TextField(20); f.add(tf); but = new Button("my button"); f.add(but); myEvent(); f.setVisible(true); } private void myEvent() { f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){ System.out.println("输入非法字符"); e.consume(); } } }); //给but添加一个键盘监听 but.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER) System.out.println("ctrl+enter is run"); //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode()); } }); /* but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("action ok"); } }); but.addMouseListener(new MouseAdapter(){ private int count = 1; private int clickCount = 1; public void mouseEntered(MouseEvent e) { System.out.println("鼠标进入到该组件"+count++); } public void mouseClicked(MouseEvent e ){ if(e.getClickCount()==2) System.out.println("双击动作"+clickCount++); } }); */ } public static void main(String[] args) { new MouseAndKeyEventDemo1(); } }
8、练习1
public class MyWindowDemo1 { private Frame f; private TextField tf; private Button but; private TextArea ta; private Dialog d; private Label lab; private Button okBut; MyWindowDemo1() { init(); } public void init() { f = new Frame("my window"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf = new TextField(60); but = new Button("转到"); ta = new TextArea(25,70); d = new Dialog(f,"提示信息-self",true); d.setBounds(400,200,240,150); d.setLayout(new FlowLayout()); lab = new Label(); okBut = new Button("确定"); d.add(lab); d.add(okBut); f.add(tf); f.add(but); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { d.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { d.setVisible(false); } }); okBut.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ d.setVisible(false); } }); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ showDir(); //System.out.println(text); //tf.setText(""); } }); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } private void showDir() { String dirPath = tf.getText(); File dir = new File(dirPath); if(dir.exists()&&dir.isDirectory()) { ta.setText(""); String[] names = dir.list(); for(String name : names) { ta.append(name+"\r\n"); } } else{ String info = "您输入的信息:"+dirPath+"是错误的,请重输"; lab.setText(info); d.setVisible(true); } } public static void main(String[] args) { new MyWindowDemo1(); } }
9、练习2
public class MyMenuTest { private Frame f ; private MenuBar bar; private TextArea ta; private Menu fileMenu; private MenuItem closeItem,saveItem,openItem; private FileDialog openDia,saveDia; private File file; MyMenuTest() { init(); } public void init() { f = new Frame("my window"); f.setBounds(300,100,650,600); //f.setLayout(new FlowLayout()); bar = new 4000 MenuBar(); ta = new TextArea(); fileMenu = new Menu("文件"); openItem = new MenuItem("打开"); saveItem = new MenuItem("保存"); closeItem = new MenuItem("退出"); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(closeItem); bar.add(fileMenu); f.setMenuBar(bar); openDia = new FileDialog(f,"我要打开",FileDialog.LOAD); saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { openItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { openDia.setVisible(true); String dirPath = openDia.getDirectory(); String fileName = openDia.getFile(); if(dirPath==null||fileName==null) return; ta.setText(""); file = new File(dirPath,fileName); try { BufferedReader bufr = new BufferedReader(new FileReader(file)); String line = null; while((line = bufr.readLine())!=null){ ta.append(line+"\r\n"); } bufr.close(); }catch(IOException ex) { throw new RuntimeException("读取失败"); } } }); saveItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(file==null){ saveDia.setVisible(true); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); if(dirPath==null||fileName==null) return; file = new File(dirPath,fileName); } try { BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); String text = ta.getText(); bufw.write(text); //bufw.flush(); bufw.close(); }catch(IOException ex) { throw new RuntimeException(""); } } }); closeItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public static void main(String[] args) { new MyMenuTest(); } }
----------------------
ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
相关文章推荐
- IOS面试题
- leetcode难度及面试频率
- 黑马程序员——字符流
- 黑马程序员 JAVA 基础篇(二)
- 面试准备汇总
- 定位+目标+通道 求职前先做好职业规划
- 黑马程序员 JAVA基础篇(一)
- 你的程序员是在努力工作还是在偷懒?
- 一位女程序员的奋斗路程
- 黑马程序员 交通灯管理系统
- JAVA面试精选
- Java面试题集(86-115)
- 给将入职场或刚入职场的人的几个建议
- 转自MITBBS. peking2 面试题总结
- 黑马程序员_WinForm基础(二)
- 奋斗不止 自强不息:职场话题之跳槽(二)—如何准备跳槽
- 程序员该有的艺术气质—SOLID原则
- 黑马程序员 银行业务调度系统
- 【程序员的吐槽】八一八那些让你感觉蠢哭了的BUG
- 建行数据中心