黑马程序员_学习笔记第21天——图形化界面

来源:互联网 发布:域名过户给企业怎么弄 编辑:程序博客网 时间:2024/05/22 11:58
---------------------- 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)

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class AwtDemo1 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Frame f = new Frame("my awt");  
  5.         f.setSize(500,400);  
  6.         f.setLocation(300,200);  
  7.         f.setLayout(new FlowLayout());  
  8.         Button b = new Button("我是一个按钮");  
  9.         f.add(b);  
  10.           
  11.         f.addWindowListener(new WindowAdapter(){  
  12.             public void windowClosing(WindowEvent e) {  
  13.                 System.out.println("我关");  
  14.                 System.exit(0);  
  15.                   
  16.             }  
  17.             public void windowActivated(WindowEvent e){  
  18.                 System.out.println("我活了");  
  19.             }  
  20.             public void windowOpened(WindowEvent e){  
  21.                 System.out.println("我被打开了。。。。");  
  22.             }  
  23.         });  
  24.         f.setVisible(true);  
  25.           
  26.   
  27.     }  
  28.   
  29. }  


7、事件监听机制组成:

事件源(组件):就是awt包或者swing包中的那些图形界面组件。

事件(Event):每一个事件源都有自己特有的对应事件和共性事件。

监听器(Listener):将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中。

注:以上三者,在java中都已经定义好了,直接获取其对象来用就可以了。

事件处理(引发事件后处理方式):对产生的动作进行处理。(实际开发中所要做的)

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class FrameDemo1 {  
  2.       
  3.     private Frame f ;  
  4.     private Button but;  
  5.       
  6.     FrameDemo1() {  
  7.         init();  
  8.     }  
  9.     public void init() {  
  10.         f = new Frame("my frame");  
  11.         //对frame进行基本设置  
  12.         f.setBounds(300,100,600,500);  
  13.         f.setLayout(new FlowLayout());  
  14.           
  15.         but = new Button("my button");  
  16.         //将组件添加到frame中  
  17.         f.add(but);  
  18.         //加载一下窗体上事件  
  19.         myEvent();  
  20.         //显示窗体  
  21.         f.setVisible(true);  
  22.     }  
  23.     private void myEvent() {  
  24.         f.addWindowListener(new WindowAdapter(){  
  25.             public void windowClosing(WindowEvent e){  
  26.                 System.exit(0);  
  27.             }  
  28.         });  
  29.         but.addActionListener(new ActionListener(){  
  30.             public void actionPerformed(ActionEvent e){  
  31.                 System.out.println("退出,按钮干的");  
  32.                 System.exit(0);  
  33.             }  
  34.         });  
  35.     }  
  36.   
  37.     public static void main(String[] args) {  
  38.         new FrameDemo1();  
  39.   
  40.     }  
  41.   
  42. }  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MouseAndKeyEventDemo1 {  
  2.     private Frame f ;  
  3.     private Button but;  
  4.     private TextField tf;  
  5.       
  6.     MouseAndKeyEventDemo1() {  
  7.         init();  
  8.     }  
  9.     public void init() {  
  10.         f = new Frame("my frame");  
  11.         f.setBounds(300,100,600,500);  
  12.         f.setLayout(new FlowLayout());  
  13.           
  14.         tf = new TextField(20);  
  15.         f.add(tf);  
  16.           
  17.         but = new Button("my button");  
  18.         f.add(but);  
  19.         myEvent();  
  20.         f.setVisible(true);  
  21.     }  
  22.     private void myEvent() {  
  23.         f.addWindowListener(new WindowAdapter(){  
  24.             public void windowClosing(WindowEvent e){  
  25.                 System.exit(0);  
  26.             }  
  27.         });  
  28.         tf.addKeyListener(new KeyAdapter(){  
  29.             public void keyPressed(KeyEvent e) {  
  30.                 int code = e.getKeyCode();  
  31.                 if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){  
  32.                     System.out.println("输入非法字符");  
  33.                     e.consume();  
  34.                 }  
  35.             }  
  36.         });  
  37.         //给but添加一个键盘监听  
  38.         but.addKeyListener(new KeyAdapter(){  
  39.             public void keyPressed(KeyEvent e){  
  40.                 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)  
  41.                     System.out.println("ctrl+enter is run");  
  42.                 //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());  
  43.             }  
  44.         });  
  45.         /* 
  46.         but.addActionListener(new ActionListener(){ 
  47.             public void actionPerformed(ActionEvent e){ 
  48.                 System.out.println("action ok"); 
  49.             } 
  50.         }); 
  51.         but.addMouseListener(new MouseAdapter(){ 
  52.             private int count = 1; 
  53.             private int clickCount = 1; 
  54.             public void mouseEntered(MouseEvent e) { 
  55.                 System.out.println("鼠标进入到该组件"+count++); 
  56.             } 
  57.             public void mouseClicked(MouseEvent e ){ 
  58.                 if(e.getClickCount()==2) 
  59.                     System.out.println("双击动作"+clickCount++); 
  60.             } 
  61.         }); 
  62.         */  
  63.     }  
  64.   
  65.     public static void main(String[] args) {  
  66.         new MouseAndKeyEventDemo1();  
  67.   
  68.     }  
  69.   
  70. }  


8、练习1

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MyWindowDemo1 {  
  2.       
  3.     private Frame f;  
  4.     private TextField tf;  
  5.     private Button but;  
  6.     private TextArea ta;  
  7.       
  8.     private Dialog d;  
  9.     private Label lab;  
  10.     private Button okBut;  
  11.       
  12.     MyWindowDemo1() {  
  13.         init();  
  14.     }  
  15.       
  16.     public void init() {  
  17.         f = new Frame("my window");  
  18.         f.setBounds(300,100,600,500);  
  19.         f.setLayout(new FlowLayout());  
  20.           
  21.         tf = new TextField(60);  
  22.           
  23.         but = new Button("转到");  
  24.           
  25.         ta = new TextArea(25,70);  
  26.           
  27.         d = new Dialog(f,"提示信息-self",true);  
  28.         d.setBounds(400,200,240,150);  
  29.         d.setLayout(new FlowLayout());  
  30.         lab = new Label();  
  31.         okBut = new Button("确定");  
  32.           
  33.         d.add(lab);  
  34.         d.add(okBut);  
  35.           
  36.         f.add(tf);  
  37.         f.add(but);  
  38.         f.add(ta);  
  39.           
  40.         myEvent();  
  41.         f.setVisible(true);  
  42.     }  
  43.     private void myEvent() {  
  44.         d.addWindowListener(new WindowAdapter(){  
  45.             public void windowClosing(WindowEvent e) {  
  46.                 d.setVisible(false);  
  47.             }  
  48.         });  
  49.         okBut.addActionListener(new ActionListener(){  
  50.             public void actionPerformed(ActionEvent e){  
  51.                 d.setVisible(false);  
  52.             }  
  53.         });  
  54.         tf.addKeyListener(new KeyAdapter(){  
  55.             public void keyPressed(KeyEvent e) {  
  56.                 if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  57.                     showDir();  
  58.             }  
  59.         });  
  60.         but.addActionListener(new ActionListener(){  
  61.             public void actionPerformed(ActionEvent e){  
  62.                   
  63.                 showDir();  
  64.                 //System.out.println(text);  
  65.                 //tf.setText("");  
  66.             }  
  67.         });  
  68.         f.addWindowListener(new WindowAdapter(){  
  69.             public void windowClosing(WindowEvent e) {  
  70.                 System.exit(0);  
  71.             }  
  72.         });  
  73.           
  74.     }  
  75.     private void showDir() {  
  76.         String dirPath = tf.getText();  
  77.           
  78.         File dir = new File(dirPath);  
  79.         if(dir.exists()&&dir.isDirectory()) {  
  80.             ta.setText("");  
  81.             String[] names = dir.list();  
  82.             for(String name : names) {  
  83.                 ta.append(name+"\r\n");  
  84.             }  
  85.         }  
  86.         else{  
  87.             String info = "您输入的信息:"+dirPath+"是错误的,请重输";  
  88.             lab.setText(info);  
  89.             d.setVisible(true);  
  90.         }  
  91.               
  92.     }  
  93.   
  94.     public static void main(String[] args) {  
  95.         new MyWindowDemo1();  
  96.   
  97.     }  
  98.   
  99. }  

9、练习2

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MyMenuTest {  
  2.     private Frame f ;  
  3.     private MenuBar bar;  
  4.     private TextArea ta;  
  5.     private Menu fileMenu;  
  6.     private MenuItem closeItem,saveItem,openItem;  
  7.       
  8.     private FileDialog openDia,saveDia;  
  9.       
  10.     private File file;  
  11.       
  12.       
  13.     MyMenuTest() {  
  14.         init();  
  15.     }  
  16.       
  17.     public void init() {  
  18.         f = new Frame("my window");  
  19.         f.setBounds(300,100,650,600);  
  20.         //f.setLayout(new FlowLayout());  
  21.           
  22.         bar = new MenuBar();  
  23.         ta = new TextArea();  
  24.           
  25.         fileMenu = new Menu("文件");  
  26.   
  27.         openItem = new MenuItem("打开");  
  28.         saveItem = new MenuItem("保存");  
  29.         closeItem = new MenuItem("退出");  
  30.           
  31.         fileMenu.add(openItem);  
  32.         fileMenu.add(saveItem);  
  33.         fileMenu.add(closeItem);  
  34.         bar.add(fileMenu);  
  35.           
  36.         f.setMenuBar(bar);  
  37.           
  38.         openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);  
  39.         saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);  
  40.           
  41.         f.add(ta);  
  42.         myEvent();  
  43.         f.setVisible(true);  
  44.     }  
  45.     private void myEvent() {  
  46.         openItem.addActionListener(new ActionListener(){  
  47.             public void actionPerformed(ActionEvent e) {  
  48.                 openDia.setVisible(true);  
  49.                 String dirPath = openDia.getDirectory();  
  50.                 String fileName = openDia.getFile();  
  51.                 if(dirPath==null||fileName==null)   
  52.                     return;  
  53.                 ta.setText("");  
  54.                 file = new File(dirPath,fileName);  
  55.                   
  56.                 try {  
  57.                     BufferedReader bufr = new BufferedReader(new FileReader(file));  
  58.                     String line = null;  
  59.                     while((line = bufr.readLine())!=null){  
  60.                         ta.append(line+"\r\n");   
  61.                     }  
  62.                     bufr.close();  
  63.                 }catch(IOException ex) {  
  64.                     throw new RuntimeException("读取失败");                   
  65.                 }  
  66.             }  
  67.         });  
  68.         saveItem.addActionListener(new ActionListener(){  
  69.             public void actionPerformed(ActionEvent e) {  
  70.                 if(file==null){  
  71.                     saveDia.setVisible(true);  
  72.                   
  73.                     String dirPath = saveDia.getDirectory();  
  74.                     String fileName = saveDia.getFile();  
  75.                     if(dirPath==null||fileName==null)   
  76.                         return;  
  77.                     file = new File(dirPath,fileName);  
  78.                 }  
  79.                 try {  
  80.                     BufferedWriter bufw = new BufferedWriter(new FileWriter(file));  
  81.                     String text = ta.getText();  
  82.                     bufw.write(text);  
  83.                     //bufw.flush();  
  84.                     bufw.close();  
  85.                 }catch(IOException ex) {  
  86.                     throw new RuntimeException("");  
  87.                 }  
  88.             }  
  89.         });  
  90.         closeItem.addActionListener(new ActionListener(){  
  91.             public void actionPerformed(ActionEvent e) {  
  92.                 System.exit(0);  
  93.             }  
  94.         });  
  95.         f.addWindowListener(new WindowAdapter(){  
  96.             public void windowClosing(WindowEvent e){  
  97.                 System.exit(0);  
  98.             }  
  99.         });  
  100.     }  
  101.   
  102.     public static void main(String[] args) {  
  103.         new MyMenuTest();  
  104.   
  105.     }  
  106.   
  107. }  



---------------------- ASP.Net+Android+IOS开发href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
0 0
原创粉丝点击